OSDN Git Service

remove
[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         }
223     }
224 }
225
226
227
228 /*--------------------------------*/
229 /* KitaThreadView actions */
230
231
232 /* private */
233 void KitaThreadTabWidget::setupActions()
234 {
235     KStdAction::copy( this, SLOT( slotCopyText() ), actionCollection() );
236
237     new KAction( i18n( "Open with Web Browser" ),
238                  0,
239                  this,
240                  SLOT( slotOpenBrowser() ),
241                  actionCollection(),
242                  "threadview_openbrowser" );
243
244     new KAction( i18n( "copy URL" ),
245                  0,
246                  this,
247                  SLOT( slotCopyURL() ),
248                  actionCollection(),
249                  "threadview_copyurl" );
250
251     new KAction( i18n( "Copy title and URL" ),
252                  0,
253                  this,
254                  SLOT( slotCopyTitleURL() ),
255                  actionCollection(),
256                  "threadview_copytitleurl" );
257
258     new KAction( i18n( "Find" ),
259                  "find",
260                  KStdAccel::find(),
261                  this,
262                  SLOT( slotFocusSearchCombo() ),
263                  actionCollection(),
264                  "threadview_find" );
265
266     KStdAction::findNext( this,
267                           SLOT( slotSearchNext() ),
268                           actionCollection(),
269                           "threadview_findnext" );
270
271     KStdAction::findPrev( this,
272                           SLOT( slotSearchPrev() ),
273                           actionCollection(),
274                           "threadview_findprev" );
275
276     new KAction( i18n( "go back anchor" ),
277                  "2leftarrow",
278                  Key_Backspace,
279                  this,
280                  SLOT( slotGobackAnchor() ),
281                  actionCollection(),
282                  "threadview_goback" );
283
284     new KAction( i18n( "Start" ),
285                  "up",
286                  Key_Home,
287                  this,
288                  SLOT( slotGotoHeader() ),
289                  actionCollection(),
290                  "threadview_gotoheader" );
291
292     new KAction( i18n( "End" ),
293                  "down",
294                  Key_End,
295                  this,
296                  SLOT( slotGotoFooter() ),
297                  actionCollection(),
298                  "threadview_gotofooter" );
299
300     new KAction( i18n( "Reload" ),
301                  "reload",
302                  KStdAccel::reload(),
303                  this,
304                  SLOT( slotReloadButton() ),
305                  actionCollection(),
306                  "threadview_reload" );
307
308     new KAction( i18n( "Stop" ),
309                  "stop",
310                  Key_Escape,
311                  this,
312                  SLOT( slotStopLoading() ),
313                  actionCollection(),
314                  "threadview_stop" );
315
316     new KAction( i18n( "Delete" ),
317                  "edittrash",
318                  Key_Delete,
319                  this,
320                  SLOT( slotDeleteButtonClicked() ),
321                  actionCollection(),
322                  "threadview_delete" );
323 }
324
325
326
327 /* KitaThreadView actions */
328
329 /* copy selected text (Ctrl+C) */ /* public slot */
330 void KitaThreadTabWidget::slotCopyText()
331 {
332     QWidget * w = currentPage();
333     KitaThreadView * view = isThreadView( w );
334     if ( view ) {
335         QClipboard * clipboard = QApplication::clipboard();
336         QString text = view->selectedText();
337         clipboard->setText( text, QClipboard::Clipboard );
338     }
339 }
340
341 /* public slot */
342 void KitaThreadTabWidget::slotOpenBrowser( int idx )
343 {
344     QWidget * w = currentPage();
345     if ( idx != -1 ) w = page( idx );
346     KURL url;
347
348     if ( w ) {
349
350         KitaThreadView * view = isThreadView( w );
351         if ( view ) {
352             new KRun( view->threadURL() );
353             return ;
354         }
355
356         KParts::Part* part = findPartFromWidget( w );
357         if ( part && part->inherits( "KParts::ReadOnlyPart" ) ) {
358             url = static_cast<KParts::ReadOnlyPart*>( part ) ->url();
359             new KRun( url );
360             return ;
361         }
362     }
363 }
364
365
366 /* public slot */
367 void KitaThreadTabWidget::slotCopyURL( int idx )
368 {
369     QWidget * w = currentPage();
370     if ( idx != -1 ) w = page( idx );
371
372     QClipboard* clipboard = QApplication::clipboard();
373
374     KitaThreadView * view = isThreadView( w );
375     if ( view ) {
376         KURL datURL = view->datURL();
377
378         QString cliptxt = Kita::DatManager::threadURL( datURL );
379         clipboard->setText( cliptxt , QClipboard::Clipboard );
380         clipboard->setText( cliptxt , QClipboard::Selection );
381     }
382 }
383
384
385 /* public slot */
386 void KitaThreadTabWidget::slotCopyTitleURL( int idx )
387 {
388     QWidget * w = currentPage();
389     if ( idx != -1 ) w = page( idx );
390
391     QClipboard* clipboard = QApplication::clipboard();
392
393     KitaThreadView * view = isThreadView( w );
394     if ( view ) {
395         KURL datURL = view->datURL();
396
397         QString cliptxt = Kita::DatManager::threadName( datURL )
398                           + "\n" + Kita::DatManager::threadURL( datURL );
399         clipboard->setText( cliptxt , QClipboard::Clipboard );
400         clipboard->setText( cliptxt , QClipboard::Selection );
401     }
402 }
403
404
405 /* public slot  */
406 void KitaThreadTabWidget::slotFocusSearchCombo()
407 {
408     KitaThreadView * view = isThreadView( currentPage() );
409     if ( view ) view->focusSearchCombo();
410 }
411
412
413 /* public slot  */
414 void KitaThreadTabWidget::slotSearchNext()
415 {
416     KitaThreadView * view = isThreadView( currentPage() );
417     if ( view ) view->slotSearchNext();
418 }
419
420
421 /* public slot  */
422 void KitaThreadTabWidget::slotSearchPrev()
423 {
424     KitaThreadView * view = isThreadView( currentPage() );
425     if ( view ) view->slotSearchPrev();
426 }
427
428
429 /* public slot  */
430 void KitaThreadTabWidget::slotGobackAnchor()
431 {
432     KitaThreadView * view = isThreadView( currentPage() );
433     if ( view ) view->slotGobackAnchor();
434 }
435
436
437 /* public slot  */
438 void KitaThreadTabWidget::slotGotoHeader()
439 {
440     KitaThreadView * view = isThreadView( currentPage() );
441     if ( view ) view->slotGotoHeader();
442 }
443
444
445 /* public slot  */
446 void KitaThreadTabWidget::slotGotoFooter()
447 {
448     KitaThreadView * view = isThreadView( currentPage() );
449     if ( view ) view->slotGotoFooter();
450 }
451
452
453 /* public slot  */
454 void KitaThreadTabWidget::slotReloadButton()
455 {
456     KitaThreadView * view = isThreadView( currentPage() );
457     if ( view ) view->slotReloadButton();
458 }
459
460
461 /* public slot  */
462 void KitaThreadTabWidget::slotStopLoading()
463 {
464     KitaThreadView * view = isThreadView( currentPage() );
465     if ( view ) view->slotStopLoading();
466 }
467
468
469 /* public slot  */
470 void KitaThreadTabWidget::slotDeleteButtonClicked()
471 {
472     KitaThreadView * view = isThreadView( currentPage() );
473     if ( view ) view->slotDeleteButtonClicked();
474 }
475
476
477 /*---------------------------------------------------------------------*/
478 /*---------------------------------------------------------------------*/
479 /*---------------------------------------------------------------------*/
480
481 /*---------------------------------------------------------------------*/
482 /*---------------------------------------------------------------------*/
483 /*---------------------------------------------------------------------*/
484
485
486 /* Don't forget to call setup later ! */
487 KitaThreadDock::KitaThreadDock( KDockManager* dockManager,
488                                 const char* name,
489                                 const QPixmap &pixmap,
490                                 QWidget* parent,
491                                 const QString& strCaption,
492                                 const QString& strTabPageLabel,
493                                 WFlags f )
494     : KitaDockWidgetBase( dockManager, name, pixmap, parent, strCaption, strTabPageLabel, f ) {}
495
496
497 KitaThreadDock::~KitaThreadDock() {}
498
499
500 /*-----------------------------*/
501 /* Main Thread View */
502
503 /* setup thig dock as main thread view */ /* public */
504 KitaThreadTabWidget* KitaThreadDock::setupAsMainView()
505 {
506     m_threadTab = new KitaThreadTabWidget( this, "Thread View" );
507     setWidget( m_threadTab );
508
509     return m_threadTab;
510 }
511
512 /* public slot */
513 void KitaThreadDock::slotShowThread( const KURL& url )
514 {
515     showDock();
516
517     if ( m_threadTab ) m_threadTab->slotShowMainThread( url );
518 }
519
520 /* show Thread Dock */ /* private slot */
521 void KitaThreadDock::slotShowThreadDock()
522 {
523     QWidget * topWidget = topLevelWidget();
524     if ( topWidget->isMinimized() ) topWidget->showNormal();
525     topWidget->raise();
526     topWidget->setActiveWindow();
527
528     slotShowDock();
529 }