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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
|
// -*- Mode: C++; c-basic-offset: 4; indent-tabs-mode: nil; tab-width: 4; -*-
//////////////////////////////////////////////////////////////////////////
// mainWidget.cpp //
// //
// Copyright (C) 2005 Lukas Tinkl <lukas@kde.org> //
// //
// 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 "mainWidget.h"
#include "tagListItem.h"
#include "bookmarkListItem.h"
#include <tqlistview.h>
#include <tqdom.h>
#include <tqpopupmenu.h>
#include <tqpushbutton.h>
#include <tqtimer.h>
#include <tqdatetime.h>
#include <kdebug.h>
#include <tdeio/job.h>
#include <krfcdate.h>
#include <tdelistview.h>
#include <klocale.h>
#include <kglobal.h>
#include <kiconloader.h>
#include <kmessagebox.h>
#include <tdeconfig.h>
#include <kinputdialog.h>
MainWidget::MainWidget( TDEConfig * config, TQWidget * parent )
: MainWidget_base( parent ), m_config( config )
{
loadTags();
TDEIconLoader * il = TDEGlobal::iconLoader();
btnRefreshTags->setIconSet( il->loadIconSet( "reload", TDEIcon::Small ) );
btnRefreshBookmarks->setIconSet( il->loadIconSet( "reload", TDEIcon::Small ) );
btnNew->setIconSet( il->loadIconSet( "bookmark_add", TDEIcon::Small ) );
connect( ( TQWidget * ) btnRefreshTags, TQT_SIGNAL( clicked() ),
this, TQT_SLOT( slotGetTags() ) );
connect( ( TQWidget * ) btnRefreshBookmarks, TQT_SIGNAL( clicked() ),
this, TQT_SLOT( slotGetBookmarks() ) );
connect( ( TQWidget * ) btnNew, TQT_SIGNAL( clicked() ),
this, TQT_SLOT( slotNewBookmark() ) );
connect( lvBookmarks, TQT_SIGNAL( executed( TQListViewItem * ) ),
this, TQT_SLOT( slotBookmarkExecuted( TQListViewItem * ) ) );
connect( lvBookmarks, TQT_SIGNAL( mouseButtonClicked ( int, TQListViewItem *, const TQPoint &, int ) ),
this, TQT_SLOT( slotBookmarkClicked( int, TQListViewItem *, const TQPoint &, int ) ) );
connect( lvTags, TQT_SIGNAL( contextMenuRequested( TQListViewItem *, const TQPoint &, int ) ),
this, TQT_SLOT( slotTagsContextMenu( TQListViewItem *, const TQPoint &, int ) ) );
connect( lvBookmarks, TQT_SIGNAL( contextMenuRequested( TQListViewItem *, const TQPoint &, int ) ),
this, TQT_SLOT( slotBookmarksContextMenu( TQListViewItem *, const TQPoint &, int ) ) );
m_updateTimer = new TQTimer( this );
connect( m_updateTimer, TQT_SIGNAL( timeout() ), TQT_SLOT( slotGetBookmarks() ) );
slotGetTags();
}
MainWidget::~MainWidget()
{
saveTags();
}
void MainWidget::setCurrentURL( const KURL & url )
{
m_currentURL = url;
}
void MainWidget::slotGetTags()
{
kdDebug() << k_funcinfo << endl;
TDEIO::StoredTransferJob * job = TDEIO::storedGet( "http://del.icio.us/api/tags/get" );
connect( job, TQT_SIGNAL( result( TDEIO::Job * ) ),
this, TQT_SLOT( slotFillTags( TDEIO::Job * ) ) );
}
void MainWidget::slotFillTags( TDEIO::Job * job )
{
kdDebug() << k_funcinfo << endl;
if ( job->error() )
{
job->showErrorDialog( this );
return;
}
lvTags->clear();
m_tags.clear();
// fill lvTags with job->data()
TQDomDocument doc;
doc.setContent( static_cast<TDEIO::StoredTransferJob *>( job )->data() );
TQDomNodeList tags = doc.elementsByTagName( "tag" );
for ( uint i = 0; i < tags.length(); ++i )
{
TQDomElement tag = tags.item( i ).toElement();
if ( !tag.isNull() )
{
TagListItem *item = new TagListItem( lvTags, tag.attribute( "tag" ), tag.attribute( "count" ).toInt() );
m_tags.append( tag.attribute( "tag" ) );
connect( item, TQT_SIGNAL( signalItemChecked( TagListItem * ) ), TQT_SLOT( itemToggled() ) );
}
}
}
void MainWidget::slotGetBookmarks()
{
KURL url( "http://del.icio.us/api/posts/recent" );
url.setQuery( "tag=" + checkedTags().join( " " ) );
kdDebug() << k_funcinfo << url.url() << endl;
TDEIO::StoredTransferJob * job = TDEIO::storedGet( url );
connect( job, TQT_SIGNAL( result( TDEIO::Job * ) ),
this, TQT_SLOT( slotFillBookmarks( TDEIO::Job * ) ) );
}
void MainWidget::slotFillBookmarks( TDEIO::Job * job )
{
kdDebug() << k_funcinfo << endl;
if ( job->error() )
{
job->showErrorDialog( this );
return;
}
lvBookmarks->clear();
// fill lvBookmarks with job->data()
TQDomDocument doc;
doc.setContent( static_cast<TDEIO::StoredTransferJob *>( job )->data() );
TQDomNodeList posts = doc.elementsByTagName( "post" );
for ( uint i = 0; i < posts.length(); ++i )
{
TQDomElement post = posts.item( i ).toElement();
if ( !post.isNull() )
{
new BookmarkListItem( lvBookmarks, post.attribute( "href" ), post.attribute( "description" ),
KRFCDate::parseDateISO8601( post.attribute( "time" ) ) );
}
}
}
TQStringList MainWidget::checkedTags() const
{
TQListViewItemIterator it( lvTags, TQListViewItemIterator::Visible | TQListViewItemIterator::Checked );
TQStringList tmp;
while ( it.current() )
{
tmp.append( it.current()->text( 0 ) );
++it;
}
return tmp;
}
void MainWidget::slotBookmarkExecuted( TQListViewItem * item )
{
BookmarkListItem * bookmark = static_cast<BookmarkListItem *>( item );
if ( bookmark )
{
kdDebug() << k_funcinfo << "Clicked bookmark URL: " << bookmark->url() << endl;
emit signalURLClicked( bookmark->url() );
}
}
void MainWidget::slotBookmarkClicked( int button, TQListViewItem * item, const TQPoint &, int )
{
BookmarkListItem * bookmark = static_cast<BookmarkListItem *>( item );
if ( bookmark && button == Qt::MidButton ) // handle middle click
{
kdDebug() << k_funcinfo << "Middle clicked bookmark URL: " << bookmark->url() << endl;
emit signalURLMidClicked( bookmark->url() );
}
}
TQStringList MainWidget::tags() const
{
return m_tags;
}
TQStringList MainWidget::bookmarks() const
{
TQListViewItemIterator it( lvBookmarks );
TQStringList tmp;
while ( it.current() )
{
tmp.append( static_cast<BookmarkListItem *>( it.current() )->url().url() );
++it;
}
return tmp;
}
void MainWidget::slotTagsContextMenu( TQListViewItem *, const TQPoint & pos, int )
{
if ( lvTags->childCount() == 0 )
return;
TQPopupMenu * tagMenu = new TQPopupMenu( this );
TQ_CHECK_PTR( tagMenu );
tagMenu->insertItem( i18n( "Check All" ), this, TQT_SLOT( slotCheckAllTags() ) );
tagMenu->insertItem( i18n( "Uncheck All" ), this, TQT_SLOT( slotUncheckAllTags() ) );
tagMenu->insertItem( i18n( "Toggle All" ), this, TQT_SLOT( slotToggleTags() ) );
tagMenu->insertSeparator();
tagMenu->insertItem( TDEGlobal::iconLoader()->loadIconSet( "edit", TDEIcon::Small ),
i18n( "Rename Tag..." ), this, TQT_SLOT( slotRenameTag() ) );
tagMenu->exec( pos );
}
void MainWidget::slotCheckAllTags()
{
TQListViewItemIterator it( lvTags );
while ( it.current() )
{
TQCheckListItem * item = static_cast<TQCheckListItem *>( *it );
if ( item )
item->setOn( true );
++it;
}
}
void MainWidget::slotUncheckAllTags()
{
TQListViewItemIterator it( lvTags );
while ( it.current() )
{
TQCheckListItem * item = static_cast<TQCheckListItem *>( *it );
if ( item )
item->setOn( false );
++it;
}
}
void MainWidget::slotToggleTags()
{
TQListViewItemIterator it( lvTags );
while ( it.current() )
{
TQCheckListItem * item = static_cast<TQCheckListItem *>( *it );
if ( item )
item->setOn( !item->isOn() );
++it;
}
}
void MainWidget::itemToggled()
{
m_updateTimer->start( 2000, true );
}
void MainWidget::slotNewBookmark()
{
emit signalURLClicked( "http://del.icio.us/post/?url=" + m_currentURL.url() );
}
void MainWidget::saveTags()
{
m_config->writeEntry( "Tags", m_tags );
}
void MainWidget::loadTags()
{
m_tags = m_config->readListEntry( "Tags" );
}
void MainWidget::slotRenameTag()
{
TagListItem * tag = static_cast<TagListItem *>( lvTags->currentItem() );
if ( tag )
{
TQString oldName = tag->name();
TQString newName = KInputDialog::getText( i18n( "Rename Tag" ), i18n( "Provide a new name for tag '%1':" ).arg( oldName ) );
if ( !newName.isEmpty() )
{
KURL url( "http://del.icio.us/api/tags/rename" );
url.addQueryItem( "old", oldName );
url.addQueryItem( "new", newName );
TDEIO::get( url ); // rename the tag
tag->setName( newName );
}
}
}
void MainWidget::slotBookmarksContextMenu( TQListViewItem *, const TQPoint & pos, int )
{
if ( lvBookmarks->childCount() == 0 )
return;
TQPopupMenu * menu = new TQPopupMenu( this );
TQ_CHECK_PTR( menu );
menu->insertItem( TDEGlobal::iconLoader()->loadIconSet( "editdelete", TDEIcon::Small ),
i18n( "Delete Bookmark" ), this, TQT_SLOT( slotDeleteBookmark() ) );
menu->exec( pos );
}
void MainWidget::slotDeleteBookmark()
{
BookmarkListItem * bookmark = static_cast<BookmarkListItem *>( lvBookmarks->currentItem() );
if ( bookmark )
{
int result = KMessageBox::warningContinueCancel( this, i18n( "Do you really want to remove the bookmark\n%1?" ).arg( bookmark->desc() ),
i18n( "Delete Bookmark" ), KStdGuiItem::del() );
if ( result == KMessageBox::Continue )
{
KURL url( "http://del.icio.us/api/posts/delete" );
url.addQueryItem( "url", bookmark->url().url() );
kdDebug() << k_funcinfo << url << endl;
TDEIO::get( url );
delete bookmark;
slotGetTags(); // re-read the tags
}
}
}
#include "mainWidget.moc"
|