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 /* 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     connect( signalCollection, SIGNAL( threadFaceChanged() ),
142              SLOT( slotFontChanged() ) );
143 }
144
145
146 /* private */
147 KitaThreadView* KitaThreadTabWidget::findMainView( const KURL& url )
148 {
149     KURL datURL = Kita::ParseMisc::parseURLonly( url );
150
151     int max = count();
152     if ( max == 0 ) return NULL;
153     int i = 0;
154
155     while ( i < max ) {
156         KitaThreadView * view = isThreadView( page ( i ) );
157         if ( view ) {
158             if ( view->getViewMode() == VIEWMODE_MAINVIEW ) {
159
160                 if ( view->datURL() == datURL
161                         || view->datURL().isEmpty()  /* default view */
162                    ) return view;
163             }
164         }
165         i++;
166     }
167
168     return NULL;
169 }
170
171
172 /* private */
173 KitaThreadView* KitaThreadTabWidget::isThreadView( QWidget* w )
174 {
175     KitaThreadView * view = NULL;
176     if ( w ) {
177         if ( w->isA( "KitaThreadView" ) ) view = static_cast< KitaThreadView* >( w );
178     }
179
180     return view;
181 }
182
183
184 /* private slots */
185 void KitaThreadTabWidget::slotUpdateThreadTab( const KURL& url )
186 {
187     KURL datURL = Kita::ParseMisc::parseURLonly( url );
188
189     KitaThreadView * view = findMainView( datURL );
190     if ( view ) {
191         QString threadName = Kita::DatManager::threadName( datURL );
192
193         setTabLabel( view, threadName );
194         setTabToolTip( view, threadName );
195     }
196 }
197
198 void KitaThreadTabWidget::slotFontChanged()
199 {
200     QFont font = Kita::Config::threadFont();
201     setFont( font );
202 }
203
204 /* protected */ /* virtual */
205 void KitaThreadTabWidget::deleteWidget( QWidget* w )
206 {
207     KitaTabWidgetBase::deleteWidget( w );
208
209     if ( count() == 0 ) {
210         ViewMediator::getInstance()->setMainCaption( QString::null );
211         ViewMediator::getInstance()->setMainStatusBar( QString::null );
212         ViewMediator::getInstance()->setMainURLLine( QString::null );
213
214         /* default view */
215         KitaThreadView * threadView = createView( "thread" );
216
217         if ( threadView ) {
218             showPage( threadView );
219         }
220     }
221 }
222
223
224
225 /*--------------------------------*/
226 /* KitaThreadView actions */
227
228
229 /* private */
230 void KitaThreadTabWidget::setupActions()
231 {
232     KStdAction::copy( this, SLOT( slotCopyText() ), actionCollection() );
233
234     new KAction( i18n( "Open with Web Browser" ),
235                  0,
236                  this,
237                  SLOT( slotOpenBrowser() ),
238                  actionCollection(),
239                  "threadview_openbrowser" );
240
241     new KAction( i18n( "copy URL" ),
242                  0,
243                  this,
244                  SLOT( slotCopyURL() ),
245                  actionCollection(),
246                  "threadview_copyurl" );
247
248     new KAction( i18n( "Copy title and URL" ),
249                  0,
250                  this,
251                  SLOT( slotCopyTitleURL() ),
252                  actionCollection(),
253                  "threadview_copytitleurl" );
254
255     new KAction( i18n( "Find" ),
256                  "find",
257                  KStdAccel::find(),
258                  this,
259                  SLOT( slotFocusSearchCombo() ),
260                  actionCollection(),
261                  "threadview_find" );
262
263     KStdAction::findNext( this,
264                           SLOT( slotSearchNext() ),
265                           actionCollection(),
266                           "threadview_findnext" );
267
268     KStdAction::findPrev( this,
269                           SLOT( slotSearchPrev() ),
270                           actionCollection(),
271                           "threadview_findprev" );
272
273     new KAction( i18n( "go back anchor" ),
274                  "2leftarrow",
275                  Key_Backspace,
276                  this,
277                  SLOT( slotGobackAnchor() ),
278                  actionCollection(),
279                  "threadview_goback" );
280
281     new KAction( i18n( "Start" ),
282                  "up",
283                  Key_Home,
284                  this,
285                  SLOT( slotGotoHeader() ),
286                  actionCollection(),
287                  "threadview_gotoheader" );
288
289     new KAction( i18n( "End" ),
290                  "down",
291                  Key_End,
292                  this,
293                  SLOT( slotGotoFooter() ),
294                  actionCollection(),
295                  "threadview_gotofooter" );
296
297     new KAction( i18n( "Reload" ),
298                  "reload",
299                  KStdAccel::reload(),
300                  this,
301                  SLOT( slotReloadButton() ),
302                  actionCollection(),
303                  "threadview_reload" );
304
305     new KAction( i18n( "Stop" ),
306                  "stop",
307                  Key_Escape,
308                  this,
309                  SLOT( slotStopLoading() ),
310                  actionCollection(),
311                  "threadview_stop" );
312
313     new KAction( i18n( "Delete" ),
314                  "edittrash",
315                  Key_Delete,
316                  this,
317                  SLOT( slotDeleteButtonClicked() ),
318                  actionCollection(),
319                  "threadview_delete" );
320 }
321
322
323
324 /* KitaThreadView actions */
325
326 /* copy selected text (Ctrl+C) */ /* public slot */
327 void KitaThreadTabWidget::slotCopyText()
328 {
329     QWidget * w = currentPage();
330     KitaThreadView * view = isThreadView( w );
331     if ( view ) {
332         QClipboard * clipboard = QApplication::clipboard();
333         QString text = view->selectedText();
334         clipboard->setText( text, QClipboard::Clipboard );
335     }
336 }
337
338 /* public slot */
339 void KitaThreadTabWidget::slotOpenBrowser( int idx )
340 {
341     QWidget * w = currentPage();
342     if ( idx != -1 ) w = page( idx );
343     KURL url;
344
345     if ( w ) {
346
347         KitaThreadView * view = isThreadView( w );
348         if ( view ) {
349             new KRun( view->threadURL() );
350             return ;
351         }
352
353         KParts::Part* part = findPartFromWidget( w );
354         if ( part && part->inherits( "KParts::ReadOnlyPart" ) ) {
355             url = static_cast<KParts::ReadOnlyPart*>( part ) ->url();
356             new KRun( url );
357             return ;
358         }
359     }
360 }
361
362
363 /* public slot */
364 void KitaThreadTabWidget::slotCopyURL( int idx )
365 {
366     QWidget * w = currentPage();
367     if ( idx != -1 ) w = page( idx );
368
369     QClipboard* clipboard = QApplication::clipboard();
370
371     KitaThreadView * view = isThreadView( w );
372     if ( view ) {
373         KURL datURL = view->datURL();
374
375         QString cliptxt = Kita::DatManager::threadURL( datURL );
376         clipboard->setText( cliptxt , QClipboard::Clipboard );
377         clipboard->setText( cliptxt , QClipboard::Selection );
378     }
379 }
380
381
382 /* public slot */
383 void KitaThreadTabWidget::slotCopyTitleURL( int idx )
384 {
385     QWidget * w = currentPage();
386     if ( idx != -1 ) w = page( idx );
387
388     QClipboard* clipboard = QApplication::clipboard();
389
390     KitaThreadView * view = isThreadView( w );
391     if ( view ) {
392         KURL datURL = view->datURL();
393
394         QString cliptxt = Kita::DatManager::threadName( datURL )
395                           + "\n" + Kita::DatManager::threadURL( datURL );
396         clipboard->setText( cliptxt , QClipboard::Clipboard );
397         clipboard->setText( cliptxt , QClipboard::Selection );
398     }
399 }
400
401
402 /* public slot  */
403 void KitaThreadTabWidget::slotFocusSearchCombo()
404 {
405     KitaThreadView * view = isThreadView( currentPage() );
406     if ( view ) view->focusSearchCombo();
407 }
408
409
410 /* public slot  */
411 void KitaThreadTabWidget::slotSearchNext()
412 {
413     KitaThreadView * view = isThreadView( currentPage() );
414     if ( view ) view->slotSearchNext();
415 }
416
417
418 /* public slot  */
419 void KitaThreadTabWidget::slotSearchPrev()
420 {
421     KitaThreadView * view = isThreadView( currentPage() );
422     if ( view ) view->slotSearchPrev();
423 }
424
425
426 /* public slot  */
427 void KitaThreadTabWidget::slotGobackAnchor()
428 {
429     KitaThreadView * view = isThreadView( currentPage() );
430     if ( view ) view->slotGobackAnchor();
431 }
432
433
434 /* public slot  */
435 void KitaThreadTabWidget::slotGotoHeader()
436 {
437     KitaThreadView * view = isThreadView( currentPage() );
438     if ( view ) view->slotGotoHeader();
439 }
440
441
442 /* public slot  */
443 void KitaThreadTabWidget::slotGotoFooter()
444 {
445     KitaThreadView * view = isThreadView( currentPage() );
446     if ( view ) view->slotGotoFooter();
447 }
448
449
450 /* public slot  */
451 void KitaThreadTabWidget::slotReloadButton()
452 {
453     KitaThreadView * view = isThreadView( currentPage() );
454     if ( view ) view->slotReloadButton();
455 }
456
457
458 /* public slot  */
459 void KitaThreadTabWidget::slotStopLoading()
460 {
461     KitaThreadView * view = isThreadView( currentPage() );
462     if ( view ) view->slotStopLoading();
463 }
464
465
466 /* public slot  */
467 void KitaThreadTabWidget::slotDeleteButtonClicked()
468 {
469     KitaThreadView * view = isThreadView( currentPage() );
470     if ( view ) view->slotDeleteButtonClicked();
471 }
472
473
474 /*---------------------------------------------------------------------*/
475 /*---------------------------------------------------------------------*/
476 /*---------------------------------------------------------------------*/
477
478 /*---------------------------------------------------------------------*/
479 /*---------------------------------------------------------------------*/
480 /*---------------------------------------------------------------------*/
481
482
483 /* Don't forget to call setup later ! */
484 KitaThreadDock::KitaThreadDock( KDockManager* dockManager,
485                                 const char* name,
486                                 const QPixmap &pixmap,
487                                 QWidget* parent,
488                                 const QString& strCaption,
489                                 const QString& strTabPageLabel,
490                                 WFlags f )
491     : KitaDockWidgetBase( dockManager, name, pixmap, parent, strCaption, strTabPageLabel, f ) {}
492
493
494 KitaThreadDock::~KitaThreadDock() {}
495
496
497 /*-----------------------------*/
498 /* Main Thread View */
499
500 /* setup thig dock as main thread view */ /* public */
501 KitaThreadTabWidget* KitaThreadDock::setupAsMainView()
502 {
503     m_threadTab = new KitaThreadTabWidget( this, "Thread View" );
504     setWidget( m_threadTab );
505
506     return m_threadTab;
507 }
508
509 /* public slot */
510 void KitaThreadDock::slotShowThread( const KURL& url )
511 {
512     showDock();
513
514     if ( m_threadTab ) m_threadTab->slotShowMainThread( url );
515 }
516
517 /* show Thread Dock */ /* private slot */
518 void KitaThreadDock::slotShowThreadDock()
519 {
520     QWidget * topWidget = topLevelWidget();
521     if ( topWidget->isMinimized() ) topWidget->showNormal();
522     topWidget->raise();
523     topWidget->setActiveWindow();
524
525     slotShowDock();
526 }