OSDN Git Service

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