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 #include "viewmediator.h"
15
16 #include "libkita/kita_misc.h"
17 #include "libkita/parsemisc.h"
18 #include "libkita/signalcollection.h"
19 #include "libkita/datmanager.h"
20 #include "libkita/boardmanager.h"
21 #include "libkita/kita-utf8.h"
22 #include "libkita/kitaconfig.h"
23 #include "libkita/config_xt.h"
24
25 #include <kdebug.h>
26 #include <kstdaccel.h>
27 #include <kaction.h>
28 #include <klocale.h>
29 #include <krun.h>
30 #include <kpopupmenu.h>
31
32 #include <qapplication.h>
33 #include <qregexp.h>
34 #include <qclipboard.h>
35 #include <qmessagebox.h>
36
37
38 /*--------------------------------------------------------------------------------*/
39
40
41 KitaThreadTabWidget::KitaThreadTabWidget( QWidget* parent, const char* name, WFlags f )
42         : KitaTabWidgetBase( parent, name, f )
43 {
44     setXMLFile( "threadtabwidgetui.rc" );
45
46     connectSignals();
47     setupActions();
48
49     /* default view */
50     createView( "thread" );
51 }
52
53
54 KitaThreadTabWidget::~KitaThreadTabWidget() {}
55
56
57 /* show "Main thread" view */  /* public slots */
58 void KitaThreadTabWidget::slotShowMainThread( const KURL& url )
59 {
60     QString refstr;
61     KURL datURL = Kita::ParseMisc::parseURL( url, refstr );
62     QString threadName = Kita::DatManager::threadName( datURL );
63     int jumpNum = 0;
64
65     int viewMode = VIEWMODE_MAINVIEW;
66     KitaThreadView* currentView = isThreadView( currentPage() );
67     if ( currentView ) viewMode = currentView->getViewMode();
68
69     if ( refstr != QString::null ) {
70         int i = refstr.find( "-" );
71         if ( i != -1 ) jumpNum = refstr.left( i ).toInt();
72         else jumpNum = refstr.toInt();
73     }
74
75     KitaThreadView* view = findMainView( datURL );
76
77     if ( view ) {
78
79         setCurrentPage( indexOf( view ) );
80
81         if ( view->threadURL().isEmpty() ) {
82             /* Show on the default view */
83             view->showThread( datURL, jumpNum );
84         } else {
85             /* The view is already shown */
86             /* TODO: jump to jumpNum after reloading */
87
88             view->slotReloadButton( jumpNum );
89         }
90
91     } else {
92         KitaThreadView * newView = createView( threadName );
93
94         if ( newView ) {
95             newView->showThread( datURL, jumpNum );
96             showPage( newView );
97         }
98     }
99
100     slotUpdateThreadTab( datURL );
101 }
102
103 /* close "all" views which URL is url. */ /* public slot */
104 void KitaThreadTabWidget::slotCloseThreadTab( const KURL& url )
105 {
106     int max = count();
107     if ( max == 0 ) return ;
108     KURL datURL = Kita::ParseMisc::parseURLonly( url );
109
110     int i, i2;
111     i = i2 = 0;
112     while ( i < max ) {
113         KitaThreadView * view = isThreadView( page ( i ) );
114         if ( view ) {
115             if ( view->datURL() == datURL ) {
116                 slotCloseTab( i2 );
117                 i2--;
118             }
119         }
120         i++; i2++;
121     }
122 }
123
124
125
126 /* create KitaThreadView */   /* private */
127 KitaThreadView* KitaThreadTabWidget::createView( QString label )
128 {
129     KitaThreadView * view = new KitaThreadView( this );
130     if ( view ) {
131         addTab( view, label );
132     }
133
134     return view;
135 }
136
137
138 /* private */
139 void KitaThreadTabWidget::connectSignals()
140 {
141     Kita::SignalCollection * signalCollection = Kita::SignalCollection::getInstance();
142
143     connect( signalCollection, SIGNAL( updateThreadTab( const KURL& ) ),
144              this, SLOT( slotUpdateThreadTab( const KURL& ) ) );
145     connect( signalCollection, SIGNAL( threadFaceChanged() ),
146              SLOT( slotFontChanged() ) );
147 }
148
149
150 /* private */
151 KitaThreadView* KitaThreadTabWidget::findMainView( const KURL& url )
152 {
153     KURL datURL = Kita::ParseMisc::parseURLonly( url );
154
155     int max = count();
156     if ( max == 0 ) return NULL;
157     int i = 0;
158
159     while ( i < max ) {
160         KitaThreadView * view = isThreadView( page ( i ) );
161         if ( view ) {
162             if ( view->getViewMode() == VIEWMODE_MAINVIEW ) {
163
164                 if ( view->datURL() == datURL
165                         || view->datURL().isEmpty()  /* default view */
166                    ) return view;
167             }
168         }
169         i++;
170     }
171
172     return NULL;
173 }
174
175
176 /* private */
177 KitaThreadView* KitaThreadTabWidget::isThreadView( QWidget* w )
178 {
179     KitaThreadView * view = NULL;
180     if ( w ) {
181         if ( w->isA( "KitaThreadView" ) ) view = static_cast< KitaThreadView* >( w );
182     }
183
184     return view;
185 }
186
187
188 /* private slots */
189 void KitaThreadTabWidget::slotUpdateThreadTab( const KURL& url )
190 {
191     KURL datURL = Kita::ParseMisc::parseURLonly( url );
192
193     KitaThreadView * view = findMainView( datURL );
194     if ( view ) {
195         QString threadName = Kita::DatManager::threadName( datURL );
196
197         setTabLabel( view, threadName );
198         setTabToolTip( view, threadName );
199     }
200 }
201
202 void KitaThreadTabWidget::slotFontChanged()
203 {
204     QFont font = Kita::Config::threadFont();
205     setFont( font );
206 }
207
208 /* protected */ /* virtual */
209 void KitaThreadTabWidget::deleteWidget( QWidget* w )
210 {
211     KitaTabWidgetBase::deleteWidget( w );
212
213     if ( count() == 0 ) {
214         emit setMainCaption( QString::null );
215         ViewMediator::getInstance()->setMainStatusBar( QString::null );
216         emit setMainURLLine( QString::null );
217
218         /* default view */
219         KitaThreadView * threadView = createView( "thread" );
220
221         if ( threadView ) {
222             showPage( threadView );
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     return m_threadTab;
511 }
512
513 /* public slot */
514 void KitaThreadDock::slotShowThread( const KURL& url )
515 {
516     showDock();
517
518     if ( m_threadTab ) m_threadTab->slotShowMainThread( url );
519 }
520
521 /* show Thread Dock */ /* private slot */
522 void KitaThreadDock::slotShowThreadDock()
523 {
524     QWidget * topWidget = topLevelWidget();
525     if ( topWidget->isMinimized() ) topWidget->showNormal();
526     topWidget->raise();
527     topWidget->setActiveWindow();
528
529     slotShowDock();
530 }