OSDN Git Service

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