summaryrefslogtreecommitdiffstats
path: root/konq-plugins/adblock/adblockdialogue.cpp
blob: 1aff0f69a182fd262aacbcd24bd59647e12df8c8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
// -*- mode: c++; c-basic-offset: 4 -*-
/*
  Copyright (C) 2006 Daniele Galdi <daniele.galdi@gmail.com>
  
  This program is free software; you can redistribute it and/or modify it
  under the terms of the GNU General Public License as published by the Free
  Software Foundation; either version 2 of the License, or (at your option)
  any later version.
  
  This program is distributed in the hope that it will be useful, but WITHOUT
  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  more details.
  
  You should have received a copy of the GNU General Public License along with
  this program; if not, write to the Free Software Foundation, Inc., 51
  Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/

#include "adblock.h"
#include "adblockdialogue.h"

#include <kdebug.h>
#include <kpopupmenu.h>
#include <klocale.h>

#include <tqcursor.h>
#include <tqlabel.h>
#include <tqvbox.h>
#include <tqlineedit.h>
#include <tqcolor.h>
#include <tqfont.h>
#include <tqpainter.h>

AdBlockDlg::AdBlockDlg(TQWidget *parent, AdElementList &elements) :
    KDialogBase( parent, "Adblock dialogue", true, "Adblock - able Items", Ok|Cancel, Ok, true )
{
    TQVBox *page = makeVBoxMainWidget();
    m_label1 = new TQLabel( i18n("All blockable items in this page:"), page, "label1" );
    
    m_list = new TQListView(page);
    m_list->setAllColumnsShowFocus( true );

    m_list->addColumn( i18n("Source") );
    m_list->addColumn( i18n("Category") );
    m_list->addColumn( i18n("Node Name") );

    m_list->setColumnWidthMode(0, TQListView::Manual);    
    m_list->setColumnWidthMode(1, TQListView::Manual);    
    m_list->setColumnWidthMode(2, TQListView::Manual);    

    m_list->setColumnWidth(0, 600);
    m_list->setColumnWidth(1, 90);
    m_list->setColumnWidth(2, 90);

    AdElementList::iterator it;
    for ( it = elements.begin(); it != elements.end(); ++it )
    {
	AdElement &element = (*it);	

	TQString url = element.url();
	
	ListViewItem *item = new ListViewItem( m_list, url, element.category(), element.type() );
	item->setBlocked(element.isBlocked());	
    }
    
    m_label2 = new TQLabel( i18n("New filter (use * as a wildcard):"), page, "label2" );
    
    m_filter = new TQLineEdit( "", page, "lineedit" );    
    
    connect(this, TQT_SIGNAL( okClicked() ), this, TQT_SLOT( validateFilter() ));
    connect(m_list, TQT_SIGNAL( doubleClicked(TQListViewItem *, const TQPoint &, int) ), this, TQT_SLOT(updateFilter(TQListViewItem *)) );

    m_menu = new KPopupMenu(this);
    m_menu->insertItem(i18n("Filter this item"), this, TQT_SLOT(filterItem()));
    m_menu->insertItem(i18n("Filter all items at same path"), this, TQT_SLOT(filterPath()));

    connect(m_list, 
	    TQT_SIGNAL( contextMenuRequested( TQListViewItem *, const TQPoint& , int ) ), 
	    this, 
	    TQT_SLOT( showContextMenu(TQListViewItem *, const TQPoint &) ) );
}

void AdBlockDlg::updateFilter(TQListViewItem *selected)
{    
    ListViewItem *item = dynamic_cast<ListViewItem *>(selected);

    if (item->isBlocked()) 
    {
	m_filter->setText("");
	return;
    }

    m_filter->setText( item->text(0) );
}

void AdBlockDlg::validateFilter()
{
    const TQString text = m_filter->text().stripWhiteSpace();

    if (!text.isEmpty())
        emit notEmptyFilter(text);

    delayedDestruct();
}

void AdBlockDlg::showContextMenu(TQListViewItem *item, const TQPoint &point)
{
    if (!item) return;
    m_menu->popup(point);
}

void AdBlockDlg::filterItem()
{
    TQListViewItem* item = m_list->selectedItem();
    m_filter->setText( item->text(0) );
}

void AdBlockDlg::filterPath()
{
    TQListViewItem* item = m_list->selectedItem();
    TQString value = item->text(0);
    m_filter->setText( value.section( '/', 0, -2 ).append("/*") );
}

AdBlockDlg::~AdBlockDlg()
{
    delete m_label1;
    delete m_label2;
    delete m_filter;
    delete m_list;
}

// ----------------------------------------------------------------------------

void ListViewItem::paintCell(TQPainter *p, const TQColorGroup & cg, int column, int width, int align)
{
    p->save();
    TQColorGroup g( cg );
    
    if ( isBlocked() )
    {
	g.setColor(TQColorGroup::Text, red);
        TQFont font;
        font.setItalic(true);
        p->setFont(font);
    }
    
    TQListViewItem::paintCell(p, g, column, width, align);
    p->restore();
}

inline bool ListViewItem::isBlocked()
{
    return m_blocked;
}

inline void ListViewItem::setBlocked(bool blocked)
{
    m_blocked = blocked;
}

#include "adblockdialogue.moc"