OSDN Git Service

refactoring.
[kita/kita.git] / kita / src / threadtabwidget.cpp
1 /***************************************************************************
2 *   Copyright (C) 2004 by Kita Developers                                 *
3 *   ikemo@users.sourceforge.jp                                            *
4 *                                                                         *
5 *   This program is free software; you can redistribute it and/or modify  *
6 *   it under the terms of the GNU General Public License as published by  *
7 *   the Free Software Foundation; either version 2 of the License, or     *
8 *   (at your option) any later version.                                   *
9 ***************************************************************************/
10
11 #include "threadtabwidget.h"
12 #include "threadview.h"
13 #include "htmlpart.h"
14
15 #include "libkita/kita_misc.h"
16 #include "libkita/parsemisc.h"
17 #include "libkita/signalcollection.h"
18 #include "libkita/datmanager.h"
19 #include "libkita/boardmanager.h"
20 #include "libkita/kita-utf8.h"
21 #include "libkita/kitaconfig.h"
22 #include "libkita/config_xt.h"
23
24 #include <kdebug.h>
25 #include <kstdaccel.h>
26 #include <kaction.h>
27 #include <klocale.h>
28 #include <krun.h>
29 #include <kpopupmenu.h>
30
31 #include <qapplication.h>
32 #include <qregexp.h>
33 #include <qclipboard.h>
34 #include <qmessagebox.h>
35
36
37 /*--------------------------------------------------------------------------------*/
38
39
40 KitaThreadTabWidget::KitaThreadTabWidget( QWidget* parent, const char* name, WFlags f )
41         : KitaTabWidgetBase( parent, name, f )
42 {
43     setXMLFile( "threadtabwidgetui.rc" );
44
45     connectSignals();
46     setupActions();
47
48     /* default view */
49     createView( "thread" );
50 }
51
52
53 KitaThreadTabWidget::~KitaThreadTabWidget() {}
54
55
56 /* show "Main thread" view */  /* public slots */
57 void KitaThreadTabWidget::slotShowMainThread( const KURL& url )
58 {
59     QString refstr;
60     KURL datURL = Kita::ParseMisc::parseURL( url, refstr );
61     QString threadName = Kita::DatManager::threadName( datURL );
62     int jumpNum = 0;
63
64     int viewMode = VIEWMODE_MAINVIEW;
65     KitaThreadView* currentView = isThreadView( currentPage() );
66     if ( currentView ) viewMode = currentView->getViewMode();
67
68     if ( refstr != QString::null ) {
69         int i = refstr.find( "-" );
70         if ( i != -1 ) jumpNum = refstr.left( i ).toInt();
71         else jumpNum = refstr.toInt();
72     }
73
74     KitaThreadView* view = findMainView( datURL );
75
76     if ( view ) {
77
78         setCurrentPage( indexOf( view ) );
79
80         if ( view->threadURL().isEmpty() ) {
81             /* Show on the default view */
82             view->showThread( datURL, jumpNum );
83         } else {
84             /* The view is already shown */
85             /* TODO: jump to jumpNum after reloading */
86
87             view->slotReloadButton( jumpNum );
88         }
89
90     } else {
91         KitaThreadView * newView = createView( threadName );
92
93         if ( newView ) {
94             newView->showThread( datURL, jumpNum );
95             showPage( newView );
96         }
97     }
98
99     slotUpdateThreadTab( datURL );
100 }
101
102 /* close "all" views which URL is url. */ /* public slot */
103 void KitaThreadTabWidget::slotCloseThreadTab( const KURL& url )
104 {
105     int max = count();
106     if ( max == 0 ) return ;
107     KURL datURL = Kita::ParseMisc::parseURLonly( url );
108
109     int i, i2;
110     i = i2 = 0;
111     while ( i < max ) {
112         KitaThreadView * view = isThreadView( page ( i ) );
113         if ( view ) {
114             if ( view->datURL() == datURL ) {
115                 slotCloseTab( i2 );
116                 i2--;
117             }
118         }
119         i++; i2++;
120     }
121 }
122
123
124
125 /* create KitaThreadView */   /* private */
126 KitaThreadView* KitaThreadTabWidget::createView( QString label )
127 {
128     KitaThreadView * view = new KitaThreadView( this );
129     if ( view ) {
130         addTab( view, label );
131     }
132
133     return view;
134 }
135
136
137 /* private */
138 void KitaThreadTabWidget::connectSignals()
139 {
140     Kita::SignalCollection * signalCollection = Kita::SignalCollection::getInstance();
141
142     connect( signalCollection, SIGNAL( updateThreadTab( const KURL& ) ),
143              this, SLOT( slotUpdateThreadTab( const KURL& ) ) );
144     connect( signalCollection, SIGNAL( threadFaceChanged() ),
145              SLOT( slotFontChanged() ) );
146 }
147
148
149 /* private */
150 KitaThreadView* KitaThreadTabWidget::findMainView( const KURL& url )
151 {
152     KURL datURL = Kita::ParseMisc::parseURLonly( url );
153
154     int max = count();
155     if ( max == 0 ) return NULL;
156     int i = 0;
157
158     while ( i < max ) {
159         KitaThreadView * view = isThreadView( page ( i ) );
160         if ( view ) {
161             if ( view->getViewMode() == VIEWMODE_MAINVIEW ) {
162
163                 if ( view->datURL() == datURL
164                         || view->datURL().isEmpty()  /* default view */
165                    ) return view;
166             }
167         }
168         i++;
169     }
170
171     return NULL;
172 }
173
174
175 /* private */
176 KitaThreadView* KitaThreadTabWidget::isThreadView( QWidget* w )
177 {
178     KitaThreadView * view = NULL;
179     if ( w ) {
180         if ( w->isA( "KitaThreadView" ) ) view = static_cast< KitaThreadView* >( w );
181     }
182
183     return view;
184 }
185
186
187 /* private slots */
188 void KitaThreadTabWidget::slotUpdateThreadTab( const KURL& url )
189 {
190     KURL datURL = Kita::ParseMisc::parseURLonly( url );
191
192     KitaThreadView * view = findMainView( datURL );
193     if ( view ) {
194         QString threadName = Kita::DatManager::threadName( datURL );
195
196         setTabLabel( view, threadName );
197         setTabToolTip( view, threadName );
198     }
199 }
200
201 void KitaThreadTabWidget::slotFontChanged()
202 {
203     QFont font = Kita::Config::threadFont();
204     setFont( font );
205 }
206
207 /* protected */ /* virtual */
208 void KitaThreadTabWidget::deleteWidget( QWidget* w )
209 {
210     KitaTabWidgetBase::deleteWidget( w );
211
212     if ( count() == 0 ) {
213         emit setMainCaption( QString::null );
214         emit setMainStatusbar( QString::null );
215         emit setMainURLLine( QString::null );
216
217         /* default view */
218         KitaThreadView * threadView = createView( "thread" );
219
220         if ( threadView ) {
221             showPage( threadView );
222             emit switchToSubject();
223         }
224     }
225 }
226
227
228
229 /*--------------------------------*/
230 /* KitaThreadView actions */
231
232
233 /* private */
234 void KitaThreadTabWidget::setupActions()
235 {
236     KStdAction::copy( this, SLOT( slotCopyText() ), actionCollection() );
237
238     new KAction( i18n( "Open with Web Browser" ),
239                  0,
240                  this,
241                  SLOT( slotOpenBrowser() ),
242                  actionCollection(),
243                  "threadview_openbrowser" );
244
245     new KAction( i18n( "copy URL" ),
246                  0,
247                  this,
248                  SLOT( slotCopyURL() ),
249                  actionCollection(),
250                  "threadview_copyurl" );
251
252     new KAction( i18n( "Copy title and URL" ),
253                  0,
254                  this,
255                  SLOT( slotCopyTitleURL() ),
256                  actionCollection(),
257                  "threadview_copytitleurl" );
258
259     new KAction( i18n( "Find" ),
260                  "find",
261                  KStdAccel::find(),
262                  this,
263                  SLOT( slotFocusSearchCombo() ),
264                  actionCollection(),
265                  "threadview_find" );
266
267     KStdAction::findNext( this,
268                           SLOT( slotSearchNext() ),
269                           actionCollection(),
270                           "threadview_findnext" );
271
272     KStdAction::findPrev( this,
273                           SLOT( slotSearchPrev() ),
274                           actionCollection(),
275                           "threadview_findprev" );
276
277     new KAction( i18n( "go back anchor" ),
278                  "2leftarrow",
279                  Key_Backspace,
280                  this,
281                  SLOT( slotGobackAnchor() ),
282                  actionCollection(),
283                  "threadview_goback" );
284
285     new KAction( i18n( "Start" ),
286                  "up",
287                  Key_Home,
288                  this,
289                  SLOT( slotGotoHeader() ),
290                  actionCollection(),
291                  "threadview_gotoheader" );
292
293     new KAction( i18n( "End" ),
294                  "down",
295                  Key_End,
296                  this,
297                  SLOT( slotGotoFooter() ),
298                  actionCollection(),
299                  "threadview_gotofooter" );
300
301     new KAction( i18n( "Reload" ),
302                  "reload",
303                  KStdAccel::reload(),
304                  this,
305                  SLOT( slotReloadButton() ),
306                  actionCollection(),
307                  "threadview_reload" );
308
309     new KAction( i18n( "Stop" ),
310                  "stop",
311                  Key_Escape,
312                  this,
313                  SLOT( slotStopLoading() ),
314                  actionCollection(),
315                  "threadview_stop" );
316
317     new KAction( i18n( "Delete" ),
318                  "edittrash",
319                  Key_Delete,
320                  this,
321                  SLOT( slotDeleteButtonClicked() ),
322                  actionCollection(),
323                  "threadview_delete" );
324 }
325
326
327
328 /* KitaThreadView actions */
329
330 /* copy selected text (Ctrl+C) */ /* public slot */
331 void KitaThreadTabWidget::slotCopyText()
332 {
333     QWidget * w = currentPage();
334     KitaThreadView * view = isThreadView( w );
335     if ( view ) {
336         QClipboard * clipboard = QApplication::clipboard();
337         QString text = view->selectedText();
338         clipboard->setText( text, QClipboard::Clipboard );
339     }
340 }
341
342 /* public slot */
343 void KitaThreadTabWidget::slotOpenBrowser( int idx )
344 {
345     QWidget * w = currentPage();
346     if ( idx != -1 ) w = page( idx );
347     KURL url;
348
349     if ( w ) {
350
351         KitaThreadView * view = isThreadView( w );
352         if ( view ) {
353             new KRun( view->threadURL() );
354             return ;
355         }
356
357         KParts::Part* part = findPartFromWidget( w );
358         if ( part && part->inherits( "KParts::ReadOnlyPart" ) ) {
359             url = static_cast<KParts::ReadOnlyPart*>( part ) ->url();
360             new KRun( url );
361             return ;
362         }
363     }
364 }
365
366
367 /* public slot */
368 void KitaThreadTabWidget::slotCopyURL( int idx )
369 {
370     QWidget * w = currentPage();
371     if ( idx != -1 ) w = page( idx );
372
373     QClipboard* clipboard = QApplication::clipboard();
374
375     KitaThreadView * view = isThreadView( w );
376     if ( view ) {
377         KURL datURL = view->datURL();
378
379         QString cliptxt = Kita::DatManager::threadURL( datURL );
380         clipboard->setText( cliptxt , QClipboard::Clipboard );
381         clipboard->setText( cliptxt , QClipboard::Selection );
382     }
383 }
384
385
386 /* public slot */
387 void KitaThreadTabWidget::slotCopyTitleURL( int idx )
388 {
389     QWidget * w = currentPage();
390     if ( idx != -1 ) w = page( idx );
391
392     QClipboard* clipboard = QApplication::clipboard();
393
394     KitaThreadView * view = isThreadView( w );
395     if ( view ) {
396         KURL datURL = view->datURL();
397
398         QString cliptxt = Kita::DatManager::threadName( datURL )
399                           + "\n" + Kita::DatManager::threadURL( datURL );
400         clipboard->setText( cliptxt , QClipboard::Clipboard );
401         clipboard->setText( cliptxt , QClipboard::Selection );
402     }
403 }
404
405
406 /* public slot  */
407 void KitaThreadTabWidget::slotFocusSearchCombo()
408 {
409     KitaThreadView * view = isThreadView( currentPage() );
410     if ( view ) view->focusSearchCombo();
411 }
412
413
414 /* public slot  */
415 void KitaThreadTabWidget::slotSearchNext()
416 {
417     KitaThreadView * view = isThreadView( currentPage() );
418     if ( view ) view->slotSearchNext();
419 }
420
421
422 /* public slot  */
423 void KitaThreadTabWidget::slotSearchPrev()
424 {
425     KitaThreadView * view = isThreadView( currentPage() );
426     if ( view ) view->slotSearchPrev();
427 }
428
429
430 /* public slot  */
431 void KitaThreadTabWidget::slotGobackAnchor()
432 {
433     KitaThreadView * view = isThreadView( currentPage() );
434     if ( view ) view->slotGobackAnchor();
435 }
436
437
438 /* public slot  */
439 void KitaThreadTabWidget::slotGotoHeader()
440 {
441     KitaThreadView * view = isThreadView( currentPage() );
442     if ( view ) view->slotGotoHeader();
443 }
444
445
446 /* public slot  */
447 void KitaThreadTabWidget::slotGotoFooter()
448 {
449     KitaThreadView * view = isThreadView( currentPage() );
450     if ( view ) view->slotGotoFooter();
451 }
452
453
454 /* public slot  */
455 void KitaThreadTabWidget::slotReloadButton()
456 {
457     KitaThreadView * view = isThreadView( currentPage() );
458     if ( view ) view->slotReloadButton();
459 }
460
461
462 /* public slot  */
463 void KitaThreadTabWidget::slotStopLoading()
464 {
465     KitaThreadView * view = isThreadView( currentPage() );
466     if ( view ) view->slotStopLoading();
467 }
468
469
470 /* public slot  */
471 void KitaThreadTabWidget::slotDeleteButtonClicked()
472 {
473     KitaThreadView * view = isThreadView( currentPage() );
474     if ( view ) view->slotDeleteButtonClicked();
475 }
476
477
478 /*---------------------------------------------------------------------*/
479 /*---------------------------------------------------------------------*/
480 /*---------------------------------------------------------------------*/
481
482 /*---------------------------------------------------------------------*/
483 /*---------------------------------------------------------------------*/
484 /*---------------------------------------------------------------------*/
485
486
487 /* Don't forget to call setup later ! */
488 KitaThreadDock::KitaThreadDock( KDockManager* dockManager,
489                                 const char* name,
490                                 const QPixmap &pixmap,
491                                 QWidget* parent,
492                                 const QString& strCaption,
493                                 const QString& strTabPageLabel,
494                                 WFlags f )
495     : KitaDockWidgetBase( dockManager, name, pixmap, parent, strCaption, strTabPageLabel, f ) {}
496
497
498 KitaThreadDock::~KitaThreadDock() {}
499
500
501 /*-----------------------------*/
502 /* Main Thread View */
503
504 /* setup thig dock as main thread view */ /* public */
505 KitaThreadTabWidget* KitaThreadDock::setupAsMainView()
506 {
507     m_threadTab = new KitaThreadTabWidget( this, "Thread View" );
508     setWidget( m_threadTab );
509
510     Kita::SignalCollection* signalCollection = Kita::SignalCollection::getInstance();
511     connect( signalCollection, SIGNAL( switchToThread() ),
512              SLOT( slotShowThreadDock() ) );
513
514     return m_threadTab;
515 }
516
517 /* public slot */
518 void KitaThreadDock::slotShowThread( const KURL& url )
519 {
520     showDock();
521
522     if ( m_threadTab ) m_threadTab->slotShowMainThread( url );
523 }
524
525 /* show Thread Dock */ /* private slot */
526 void KitaThreadDock::slotShowThreadDock()
527 {
528     QWidget * topWidget = topLevelWidget();
529     if ( topWidget->isMinimized() ) topWidget->showNormal();
530     topWidget->raise();
531     topWidget->setActiveWindow();
532
533     slotShowDock();
534 }