OSDN Git Service

use KConfig XT
[kita/kita.git] / kita / src / mainwindow.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 "mainwindow.h"
12
13 #include "prefs/prefs.h"
14
15 #include "thread/threadview.h"
16 #include "thread/threadtabwidget.h"
17
18 #include "board/boardview.h"
19 #include "board/boardtabwidget.h"
20 #include "bbs/bbsview.h"
21 #include "bbs/bbstabwidget.h"
22 #include "favoriteswindow.h"
23
24 #include "write/writetabwidget.h"
25
26 #include "libkita/threadinfo.h"
27 #include "libkita/favoriteboards.h"
28 #include "libkita/favoritethreads.h"
29 #include "libkita/kita_misc.h"
30 #include "libkita/kitaconfig.h"
31 #include "libkita/signalcollection.h"
32 #include "libkita/account.h"
33 #include "libkita/imgmanager.h"
34 #include "libkita/datmanager.h"
35 #include "libkita/boardmanager.h"
36 #include "libkita/config_xt.h"
37 #include "libkita/asciiart.h"
38 #include "libkita/abone.h"
39
40 #include "image/imagetabwidget.h"
41
42 #include <qdragobject.h>
43 #include <qtextcodec.h>
44 #include <qevent.h>
45 #include <qregexp.h>
46 #include <qmessagebox.h>
47 #include <qclipboard.h>
48
49 #include <klineedit.h>
50 #include <kglobal.h>
51 #include <klocale.h>
52 #include <kiconloader.h>
53 #include <kmenubar.h>
54 #include <kstatusbar.h>
55 #include <kkeydialog.h>
56 #include <kaccel.h>
57 #include <kio/netaccess.h>
58 #include <kuserprofile.h>
59 #include <kfiledialog.h>
60 #include <kconfig.h>
61 #include <kurl.h>
62 #include <krun.h>
63 #include <kstandarddirs.h>
64 #include <kdebug.h>
65 #include <ksimpleconfig.h>
66 #include <kedittoolbar.h>
67 #include <kstdaccel.h>
68 #include <kaction.h>
69 #include <kstdaction.h>
70 #include <kshortcut.h>
71 #include <kaboutdata.h>
72 #include <kparts/partmanager.h>
73 #include <kdeversion.h>
74 #include <kglobal.h>
75
76 KitaMainWindow::KitaMainWindow()
77         : KParts::DockMainWindow( 0, "Kita" ),
78         m_printer( 0 )
79 {
80     // FIXME: merge *.po
81     KGlobal::locale() ->insertCatalogue( "kitapart" );
82
83     // accept dnd
84     setAcceptDrops( true );
85
86     // setup imgmanager
87     Kita::ImgManager::setup( this );
88
89     // setup view, dock
90     setupView();
91
92     // then, setup our actions
93     setupActions();
94
95     // load cache
96     loadCache();
97
98     // load style sheet
99     loadStyleSheet();
100
101     // load ascii art
102     Kita::AsciiArtConfig::self()->readConfig();
103     if ( Kita::AsciiArtConfig::self()->asciiArtList().empty() ) {
104         loadAsciiArt();
105     }
106
107     // load abone lists
108     Kita::AboneConfig::self()->readConfig();
109     if ( Kita::AboneConfig::self()->aboneIDList().empty() ) {
110         loadAboneIDList();
111     }
112
113     if ( Kita::AboneConfig::self()->aboneNameList().empty() ) {
114         loadAboneNameList();
115     }
116
117     if ( Kita::AboneConfig::self()->aboneWordList().empty() ) {
118         loadAboneWordList();
119     }
120
121     // and a status bar
122     statusBar() ->show();
123
124     // read dock status
125     m_bbsDock->loadSession();
126     m_boardDock->loadSession();
127     m_threadDock->loadSession();
128     m_naviDock->loadSession();
129     m_imageDock->loadSession();
130     m_writeDock->loadSession();
131     QString dockConfigPath = locateLocal( "appdata", "dock.conf" );
132     KConfig dockConfig( dockConfigPath );
133     readDockConfig( &dockConfig );
134
135     // show subject dock
136     m_boardDock->slotShowDock();
137
138     // show thread view
139     m_threadDock->slotShowDock();
140
141     // hide writedock
142     makeDockInvisible( m_writeDock );
143     m_showImgViewAction->setChecked( FALSE );
144
145     // hide kitanavi
146     makeDockInvisible( m_naviDock );
147     m_showNaviAction->setChecked( FALSE );
148
149     // hide imageviewer
150     makeDockInvisible( m_imageDock );
151     m_showImgViewAction->setChecked( FALSE );
152
153
154     KitaConfig::readConfig( KGlobal::config() );
155     Kita::Config::self()->readConfig();
156
157     if ( Kita::Config::self()->nameCompletionList().empty() ) {
158         loadCompletion();
159     }
160
161     // apply the saved mainwindow settings, if any, and ask the mainwindow
162     // to automatically save settings if changed: window size, toolbar
163     // position, icon size, etc.
164     setAutoSaveSettings();
165
166     // set list font
167     setFont( Kita::Config::font() );
168
169     // allow the view to change the statusbar and caption
170     Kita::SignalCollection* signalCollection = Kita::SignalCollection::getInstance();
171
172     connect( signalCollection, SIGNAL( openURLRequest( const KURL&, const KParts::URLArgs& ) ),
173              SLOT( slotOpenURLRequest( const KURL&, const KParts::URLArgs& ) ) );
174
175     connect( signalCollection, SIGNAL( openURLRequestExt(
176                                            const KURL&, const KParts::URLArgs&, QString, int,
177                                            const KURL& ) ),
178              SLOT( slotOpenURLRequestExt(
179                        const KURL& , const KParts::URLArgs&, QString, int,
180                        const KURL& ) ) );
181
182     connect( signalCollection, SIGNAL( setMainCaption( const QString& ) ),
183              SLOT( slotSetMainCaption( const QString& ) ) );
184
185     connect( signalCollection, SIGNAL( setMainStatusbar( const QString& ) ),
186              SLOT( slotSetMainStatusbar( const QString& ) ) );
187
188     connect( signalCollection, SIGNAL( setMainURLLine( const KURL& ) ),
189              SLOT( setUrl( const KURL& ) ) );
190
191     connect( signalCollection, SIGNAL( bookmarked( const QString&, bool ) ),
192              SLOT( bookmark( const QString&, bool ) ) );
193
194     connect( this, SIGNAL( favoritesUpdated() ),
195              signalCollection, SIGNAL( favoritesUpdated() ) );
196
197     connect( m_urlLine, SIGNAL( returnPressed() ),
198              SLOT( slotURLLine() ) );
199
200     QPopupMenu* settingsPopup = static_cast<QPopupMenu *>( factory() ->container( "settings", this ) );
201     connect( settingsPopup, SIGNAL( aboutToShow() ),
202              SLOT( settingsMenuAboutToShow() ) );
203
204     // load favorite boards;
205     loadFavoriteBoards();
206
207     // load boad list
208     {
209         QString configPath = locateLocal( "appdata", "board_list" );
210         if ( QFile::exists( configPath ) ) {
211             m_bbsTab->showBoardList();
212             m_bbsTab->loadOpened();
213         } else {
214             m_bbsTab->updateBoardList();
215         }
216     }
217
218     // load favorites
219     loadFavorites();
220
221     // show board dock
222     m_bbsDock->slotShowDock();
223
224     // update favorite list
225     emit favoritesUpdated();
226
227     if ( Kita::Config::autoLogin() ) {
228         login();
229     }
230 }
231
232 KitaMainWindow::~KitaMainWindow()
233 {
234     /* save dock status */
235     m_bbsDock->saveSession();
236     m_boardDock->saveSession();
237     m_threadDock->saveSession();
238     m_naviDock->saveSession();
239     m_imageDock->saveSession();
240     m_writeDock->saveSession();
241
242     Kita::AboneConfig::self()->writeConfig();
243
244     saveStyleSheet();
245
246     Kita::AsciiArtConfig::self()->writeConfig();
247
248     saveFavoriteBoards();
249
250     saveFavorites();
251
252     saveCache();
253
254     saveMainWindowSettings( KGlobal::config(), "MainWindow" );
255
256     KitaConfig::writeConfig( KGlobal::config() );
257     Kita::Config::self()->writeConfig();
258
259     QString dockConfigPath = locateLocal( "appdata", "dock.conf" );
260     KConfig dockConfig( dockConfigPath );
261     writeDockConfig( &dockConfig );
262
263     delete m_bbsDock;
264     delete m_threadDock;
265     delete m_naviDock;
266     delete m_imageDock;
267     delete m_writeDock;
268
269     Kita::DatManager::deleteAllDatInfo();
270 }
271
272 void KitaMainWindow::load( const KURL& url )
273 {
274     QString target;
275     // the below code is what you should normally do.  in this
276     // example case, we want the url to our own.  you probably
277     // want to use this code instead for your app
278
279 #if 0
280     // download the contents
281     if ( KIO::NetAccess::download( url, target ) ) {
282         // set our caption
283         setCaption( url );
284
285         // load in the file (target is always local)
286         loadFile( target );
287
288         // and remove the temp file
289         KIO::NetAccess::removeTempFile( target );
290     }
291 #endif
292
293     setCaption( url.url() );
294     //    m_view->openURL(url);
295 }
296
297 void KitaMainWindow::setupActions()
298 {
299     KStdAction::quit( this, SLOT( close() ), actionCollection() );
300     KStdAction::copy( this, SLOT( slotEditCopy() ), actionCollection() );
301
302     m_toolbarAction = KStdAction::showToolbar( this,
303                       SLOT( optionsShowToolbar() ),
304                       actionCollection() );
305
306     m_statusbarAction = KStdAction::showStatusbar( this,
307                         SLOT( optionsShowStatusbar() ),
308                         actionCollection() );
309
310     m_boardListAction = new KToggleAction( i18n( "Show board list" ),
311                                            0,
312                                            m_bbsDock,
313                                            SLOT( slotToggleShowHide() ),
314                                            actionCollection(),
315                                            "window_show_board_list" );
316
317     m_subjectListAction = new KToggleAction( i18n( "Show subject list" ),
318                           0,
319                           m_boardDock,
320                           SLOT( slotToggleShowHide() ),
321                           actionCollection(),
322                           "window_show_subject_list" );
323
324     m_showImgViewAction = new KToggleAction( i18n( "Show Image Viewer" ),
325                           0,
326                           m_imageDock,
327                           SLOT( slotToggleShowHide() ),
328                           actionCollection(),
329                           "window_show_imgviewer" );
330
331     m_showNaviAction = new KToggleAction( i18n( "Show KitaNavi" ),
332                                           0,
333                                           m_naviDock,
334                                           SLOT( slotToggleShowHide() ),
335                                           actionCollection(),
336                                           "window_show_kitanavi" );
337
338     m_showWriteDockAction = new KToggleAction( i18n( "Show Write Dock" ),
339                             0,
340                             m_writeDock,
341                             SLOT( slotToggleShowHide() ),
342                             actionCollection(),
343                             "window_show_writedock" );
344
345     m_urlLine = new KLineEdit( "", 0 );
346
347     new KWidgetAction( m_urlLine,
348                        i18n( "URL Line" ),
349                        0,
350                        this,
351                        SLOT( slotURLLine() ),
352                        actionCollection(), "url_line_action" );
353
354     KStdAction::keyBindings( this, SLOT( optionsConfigureKeys() ), actionCollection() );
355     KStdAction::configureToolbars( this, SLOT( optionsConfigureToolbars() ), actionCollection() );
356     KStdAction::preferences( this, SLOT( optionsPreferences() ), actionCollection() );
357
358     /*    new KAction( i18n( "Edit Favorites" ),
359                      0,
360                      this,
361                      SLOT( favoritesEdit() ),
362                      actionCollection(),
363                      "favorites_edit" );*/
364
365     new KAction( i18n( "Load board list" ),
366                  0,
367                  m_bbsTab,
368                  SLOT( updateBoardList() ),
369                  actionCollection(),
370                  "load_board_list" );
371
372     new KAction( i18n( "reset windows" ),
373                  0,
374                  this,
375                  SLOT( resetWindowsCust() ),
376                  actionCollection(),
377                  "reset_wincust" );
378
379     new KAction( i18n( "default 1" ),
380                  0,
381                  this,
382                  SLOT( resetWindowsDef1() ),
383                  actionCollection(),
384                  "reset_windef1" );
385
386     new KAction( i18n( "default 2" ),
387                  0,
388                  this,
389                  SLOT( resetWindowsDef2() ),
390                  actionCollection(),
391                  "reset_windef2" );
392
393     new KAction( i18n( "default 3" ),
394                  0,
395                  this,
396                  SLOT( resetWindowsDef3() ),
397                  actionCollection(),
398                  "reset_windef3" );
399
400     m_toggleViewAction = new KAction(
401                              i18n( "&Toggle subject view and thread view" ),
402                              KShortcut( Qt::Key_F7 ),
403                              this,
404                              SLOT( windowToggleView() ),
405                              actionCollection(),
406                              "window_toggle_view" );
407
408     new KAction( i18n( "Login" ),
409                  0,
410                  this,
411                  SLOT( login() ),
412                  actionCollection(),
413                  "login" );
414
415     setXMLFile( "kitaui.rc" );
416     KMainWindow::createGUI();
417     factory() ->addClient( m_bbsTab );
418     factory() ->addClient( m_boardTab );
419     factory() ->addClient( m_threadTab );
420     factory() ->addClient( m_imageTab );
421     factory() ->addClient( m_naviTab );
422     factory() ->addClient( m_writeTab );
423 }
424
425 void KitaMainWindow::slotURLLine()
426 {
427     KURL url = m_urlLine->text();
428     KURL datURL = Kita::getDatURL( url );
429     m_threadDock->slotShowThread( datURL, Kita::Config::alwaysUseTab() );
430 }
431
432 void KitaMainWindow::saveProperties( KConfig* )
433 {
434     // the 'config' object points to the session managed
435     // config file.  anything you write here will be available
436     // later when this app is restored
437
438     //    if (m_view->currentURL() != QString::null)
439     //        config->writeEntry("lastURL", m_view->currentURL());
440     //    KitaConfig::writeConfig( config );
441 }
442
443 void KitaMainWindow::readProperties( KConfig* )
444 {
445     // the 'config' object points to the session managed
446     // config file.  this function is automatically called whenever
447     // the app is being restored.  read in here whatever you wrote
448     // in 'saveProperties'
449
450     //    QString url = config->readEntry("lastURL");
451
452     //    if (url != QString::null)
453     //        m_view->openURL(KURL(url));
454     //    KitaConfig::readConfig( config );
455 }
456
457 void KitaMainWindow::dragEnterEvent( QDragEnterEvent* event )
458 {
459     // accept uri drops only
460     event->accept( QUriDrag::canDecode( event ) );
461 }
462
463 void KitaMainWindow::dropEvent( QDropEvent* event )
464 {
465     // this is a very simplistic implementation of a drop event.  we
466     // will only accept a dropped URL.  the Qt dnd code can do *much*
467     // much more, so please read the docs there
468     QStrList uri;
469
470     // see if we can decode a URI.. if not, just ignore it
471     if ( QUriDrag::decode( event, uri ) ) {
472         // okay, we have a URI.. process it
473         QString url, target;
474         url = uri.first();
475
476         // load in the file
477         load( KURL( url ) );
478     }
479 }
480
481 void KitaMainWindow::optionsShowToolbar()
482 {
483     // this is all very cut and paste code for showing/hiding the
484     // toolbar
485     if ( m_toolbarAction->isChecked() ) {
486         toolBar() ->show();
487     } else {
488         toolBar() ->hide();
489     }
490 }
491
492 void KitaMainWindow::optionsShowStatusbar()
493 {
494     // this is all very cut and paste code for showing/hiding the
495     // statusbar
496     if ( m_statusbarAction->isChecked() ) {
497         statusBar() ->show();
498     } else {
499         statusBar() ->hide();
500     }
501 }
502
503 void KitaMainWindow::optionsConfigureKeys()
504 {
505     KKeyDialog dlg( TRUE, this );
506     dlg.insert( actionCollection(), "Kita" );
507     dlg.configure();
508 }
509
510 void KitaMainWindow::optionsConfigureToolbars()
511 {
512     // use the standard toolbar editor
513     KEditToolbar dlg( factory() );
514     connect( &dlg, SIGNAL( newToolbarConfig() ),
515              SLOT( newToolbarConfig() ) );
516     dlg.exec();
517 }
518
519 void KitaMainWindow::newToolbarConfig()
520 {
521     // this slot is called when user clicks "Ok" or "Apply" in the toolbar editor.
522     // recreate our GUI, and re-apply the settings (e.g. "text under icons", etc.)
523     applyMainWindowSettings( KGlobal::config(), "MainWindow" );
524 }
525
526 void KitaMainWindow::resetWindowsCust() { resetWindows( 0 ); }
527 void KitaMainWindow::resetWindowsDef1() { resetWindows( 1 ); }
528 void KitaMainWindow::resetWindowsDef2() { resetWindows( 2 ); }
529 void KitaMainWindow::resetWindowsDef3() { resetWindows( 3 ); }
530
531 void KitaMainWindow::resetWindows( int pattern )
532 {
533     if ( !m_dummyDock || !m_threadDock || !m_bbsDock
534             || !m_boardDock || !m_naviDock || !m_imageDock || !m_writeDock ) return ;
535
536     makeDockVisible( m_threadDock );
537     makeDockVisible( m_bbsDock );
538     makeDockVisible( m_boardDock );
539     makeDockVisible( m_naviDock );
540     makeDockVisible( m_imageDock );
541     makeDockVisible( m_writeDock );
542
543     makeDockInvisible( m_threadDock );
544     makeDockInvisible( m_bbsDock );
545     makeDockInvisible( m_boardDock );
546     makeDockInvisible( m_naviDock );
547     makeDockInvisible( m_imageDock );
548     makeDockInvisible( m_writeDock );
549
550     m_threadDock->manualDock( m_dummyDock, KDockWidget::DockTop, 100 );
551     m_bbsDock->manualDock( m_threadDock, KDockWidget::DockLeft, 20 );
552     m_boardDock->manualDock( m_threadDock, KDockWidget::DockTop, 25 );
553     m_imageDock->manualDock( m_threadDock, KDockWidget::DockTop, 25 );
554     m_writeDock->manualDock( m_threadDock, KDockWidget::DockBottom, 75 );
555     m_naviDock->manualDock( m_threadDock, KDockWidget::DockBottom, 50 );
556
557     switch ( pattern ) {
558
559         /*----------------------------------------*/
560     case 0:
561
562         /* setSession( docked, tabbed ) */
563         m_threadDock ->setSession( TRUE, FALSE );
564         m_bbsDock ->setSession( TRUE, FALSE );
565         m_boardDock->setSession( TRUE, FALSE );
566         m_imageDock ->setSession( TRUE, FALSE );
567         m_writeDock ->setSession( TRUE, FALSE );
568         m_naviDock ->setSession( TRUE, FALSE );
569
570         if ( m_boardListAction ) m_boardListAction->setChecked( TRUE );
571         if ( m_subjectListAction ) m_subjectListAction->setChecked( TRUE );
572         if ( m_showImgViewAction ) m_showImgViewAction->setChecked( TRUE );
573         if ( m_showNaviAction ) m_showNaviAction->setChecked( TRUE );
574         if ( m_showWriteDockAction ) m_showWriteDockAction->setChecked( TRUE );
575
576         break;
577
578         /*----------------------------------------*/
579     case 1:
580
581         m_imageDock->toDesktop();
582         m_writeDock->toDesktop();
583         m_naviDock->toDesktop();
584
585         makeDockInvisible( m_imageDock );
586         makeDockInvisible( m_writeDock );
587         makeDockInvisible( m_naviDock );
588
589         /* setSession( docked, tabbed ) */
590         m_threadDock ->setSession( TRUE, FALSE );
591         m_bbsDock ->setSession( TRUE, FALSE );
592         m_boardDock->setSession( TRUE, FALSE );
593         m_imageDock ->setSession( FALSE, FALSE );
594         m_writeDock ->setSession( FALSE, FALSE );
595         m_naviDock ->setSession( FALSE, FALSE );
596
597         if ( m_boardListAction ) m_boardListAction->setChecked( TRUE );
598         if ( m_subjectListAction ) m_subjectListAction->setChecked( TRUE );
599         if ( m_showImgViewAction ) m_showImgViewAction->setChecked( FALSE );
600         if ( m_showNaviAction ) m_showNaviAction->setChecked( FALSE );
601         if ( m_showWriteDockAction ) m_showWriteDockAction->setChecked( FALSE );
602
603         break;
604
605         /*----------------------------------------*/
606     case 2:
607
608         m_boardDock->manualDock( m_threadDock, KDockWidget::DockCenter, 0 );
609
610         m_imageDock->toDesktop();
611         m_writeDock->toDesktop();
612         m_naviDock->toDesktop();
613
614         makeDockInvisible( m_imageDock );
615         makeDockInvisible( m_writeDock );
616         makeDockInvisible( m_naviDock );
617
618         /* setSession( docked, tabbed ) */
619         m_threadDock ->setSession( TRUE, TRUE );
620         m_bbsDock ->setSession( TRUE, FALSE );
621         m_boardDock->setSession( TRUE, TRUE );
622         m_imageDock ->setSession( FALSE, FALSE );
623         m_writeDock ->setSession( FALSE, FALSE );
624         m_naviDock ->setSession( FALSE, FALSE );
625
626         if ( m_boardListAction ) m_boardListAction->setChecked( TRUE );
627         if ( m_subjectListAction ) m_subjectListAction->setChecked( TRUE );
628         if ( m_showImgViewAction ) m_showImgViewAction->setChecked( FALSE );
629         if ( m_showNaviAction ) m_showNaviAction->setChecked( FALSE );
630         if ( m_showWriteDockAction ) m_showWriteDockAction->setChecked( FALSE );
631
632         break;
633
634         /*----------------------------------------*/
635     case 3:
636
637         m_boardDock->manualDock( m_threadDock, KDockWidget::DockCenter, 0 );
638         m_naviDock->manualDock( m_threadDock, KDockWidget::DockCenter, 0 );
639         m_imageDock->manualDock( m_threadDock, KDockWidget::DockCenter, 0 );
640
641         makeDockInvisible( m_imageDock );
642         makeDockInvisible( m_writeDock );
643         makeDockInvisible( m_naviDock );
644
645         /* setSession( docked, tabbed ) */
646         m_threadDock ->setSession( TRUE, TRUE );
647         m_bbsDock ->setSession( TRUE, FALSE );
648         m_boardDock->setSession( TRUE, TRUE );
649         m_imageDock ->setSession( TRUE, TRUE );
650         m_writeDock ->setSession( TRUE, FALSE );
651         m_naviDock ->setSession( TRUE, TRUE );
652
653         if ( m_boardListAction ) m_boardListAction->setChecked( TRUE );
654         if ( m_subjectListAction ) m_subjectListAction->setChecked( TRUE );
655         if ( m_showImgViewAction ) m_showImgViewAction->setChecked( FALSE );
656         if ( m_showNaviAction ) m_showNaviAction->setChecked( FALSE );
657         if ( m_showWriteDockAction ) m_showWriteDockAction->setChecked( FALSE );
658
659         break;
660     }
661
662     m_threadDock->slotShowDock();
663 }
664
665
666 void KitaMainWindow::windowToggleView()
667 {
668     //FIXME: m_boardDock->dockSite() & KDockWidget::DockCenter != 0 igai no toki ha dousuru?
669     kdDebug() << "Do toggle" << endl;
670     if ( !m_boardDock->isVisible()
671             || !m_boardDock->isActiveWindow() ) m_boardDock->slotShowDock();
672     else {
673         m_threadDock->slotShowDock();
674     }
675 }
676
677 void KitaMainWindow::settingsMenuAboutToShow()
678 {
679     m_toolbarAction->setChecked( toolBar() ->isVisible() );
680     m_statusbarAction->setChecked( statusBar() ->isVisible() );
681 }
682
683 void KitaMainWindow::optionsPreferences()
684 {
685     // popup some sort of preference dialog, here
686     if ( KConfigDialog::showDialog( "Kita Preferences" ) ) {
687         return;
688     }
689
690     KitaPreferences* dialog = new KitaPreferences( this );
691
692     connect( dialog, SIGNAL( fontChanged( const QFont& ) ),
693              SLOT( setFont( const QFont& ) ) );
694
695     dialog->show();
696 }
697
698 void KitaMainWindow::slotSetMainStatusbar( const QString& statusStr )
699 {
700     // display the text on the statusbar
701     statusBar() ->message( statusStr );
702 }
703
704 void KitaMainWindow::setupView()
705 {
706     /* dummy Main Dock Widget */
707     m_dummyDock = createDockWidget( "dummy", 0L, 0L, "dummy", "dummy" );
708     m_dummyDock->setDockSite( KDockWidget::DockFullSite );
709     m_dummyDock->setEnableDocking( KDockWidget::DockNone );
710     setView( m_dummyDock );
711     setMainDockWidget( m_dummyDock );
712
713     /* Subject View */
714     m_boardDock = new KitaBoardDock( manager(), "subject", 0L, 0L, i18n( "subject" ), i18n( "subject" ) );
715     m_boardTab = m_boardDock->setup();
716     connect( m_boardDock, SIGNAL( checkToggleAction( bool ) ), SLOT( slotCheckSubjectToggleAction( bool ) ) );
717
718     /* Board View */
719     m_bbsDock = new KitaBBSDock( manager(), "board", 0L, 0L, i18n( "board" ), i18n( "board" ) );
720     m_bbsTab = m_bbsDock->setup();
721     connect( m_bbsDock, SIGNAL( checkToggleAction( bool ) ), SLOT( slotCheckBoardToggleAction( bool ) ) );
722
723     /* Thread View */
724     m_threadDock = new KitaThreadDock( manager(), "Thread", 0L, 0L, i18n( "Thread" ), i18n( "Thread" ) );
725     m_threadTab = m_threadDock->setupAsMainView();
726     m_threadDock->setDockSite( KDockWidget::DockFullSite );
727
728     /* KitaNavi */
729     m_naviDock = new KitaThreadDock( manager(), "kitanavi", 0L, 0L, i18n( "KitaNavi" ), i18n( "Navi" ) );
730     m_naviTab = m_naviDock->setupAsKitaNavi();
731     connect( m_naviDock, SIGNAL( checkToggleAction( bool ) ), SLOT( slotCheckNaviToggleAction( bool ) ) );
732
733     if ( !Kita::Config::useKitaNavi() ) m_threadDock ->connectNaviSignals();
734     else m_naviDock->connectNaviSignals();
735
736     /* KitaImgViewer */
737     m_imageDock = new KitaImgDock( manager(), "imgviewer", 0L, 0L, i18n( "Kita Image Viewer" ), i18n( "Image" ) );
738     m_imageTab = m_imageDock->setup();
739     connect( m_imageDock, SIGNAL( checkToggleAction( bool ) ), SLOT( slotCheckImgToggleAction( bool ) ) );
740
741     /* write dock */
742     m_writeDock = new KitaWriteDock( manager(), "Write", 0L, 0L, i18n( "Write" ), i18n( "Write" ) );
743     m_writeTab = m_writeDock->setup();
744     connect( m_writeDock, SIGNAL( checkToggleAction( bool ) ), SLOT( slotCheckWriteToggleAction( bool ) ) );
745
746     /* reset dock status */
747     m_boardListAction = NULL;
748     m_subjectListAction = NULL;
749     m_showImgViewAction = NULL;
750     m_showNaviAction = NULL;
751     m_showWriteDockAction = NULL;
752     resetWindowsDef1();
753 }
754
755 void KitaMainWindow::closeSubjectView()
756 {
757     m_subjectListAction->setChecked( FALSE );
758 }
759
760 void KitaMainWindow::slotCheckBoardToggleAction( bool check )
761 {
762     if ( m_boardListAction ) m_boardListAction->setChecked( check );
763 }
764
765 void KitaMainWindow::slotCheckSubjectToggleAction( bool check )
766 {
767     if ( m_subjectListAction ) m_subjectListAction->setChecked( check );
768 }
769
770 void KitaMainWindow::slotCheckNaviToggleAction( bool check )
771 {
772     if ( m_showNaviAction ) m_showNaviAction->setChecked( check );
773 }
774
775 void KitaMainWindow::slotCheckImgToggleAction( bool check )
776 {
777     if ( m_showImgViewAction ) m_showImgViewAction->setChecked( check );
778 }
779
780
781 void KitaMainWindow::slotCheckWriteToggleAction( bool check )
782 {
783     if ( m_showWriteDockAction ) m_showWriteDockAction->setChecked( check );
784 }
785
786
787 /* open url */ /* public slot */ /* obsolete */
788 void KitaMainWindow::slotOpenURLRequest( const KURL& url, const KParts::URLArgs& args )
789 {
790     slotOpenURLRequestExt( url, args );
791 }
792
793
794 /* open url */
795 /*
796   If mimetype == "kita_open_2chboard", open the board.
797   If mimetype == "kita_open_2chthread",  open the thread.
798   If mimetype == "text/html", open url with web browser.
799   if mimetype == QString::null, use KIO::NetAccess::mimetype to decide mime type of url.
800  
801 */  /* public slot */
802 void KitaMainWindow::slotOpenURLRequestExt(
803     const KURL& url, const KParts::URLArgs& args,
804     QString mimetype,
805     int usr1,
806     const KURL& usrUrl1 )
807 {
808     // FIXME: support be.2ch.net
809     if ( url.host() == "be.2ch.net" && url.path().contains( "/test/p.php", true ) < 1 ) {
810         QMessageBox::warning( this,
811                               "Kita",
812                               i18n( "be.2ch.net is currently not supported." ) );
813         return ;
814     }
815
816     kdDebug() << QString( "Requested OpenURL: \"%1\"" ).arg( url.url() ) << endl;
817     kdDebug() << QString( "content type: \"%1\"" ).arg( args.contentType() ) << endl;
818     kdDebug() << QString( "service type: \"%1\"" ).arg( args.serviceType ) << endl;
819     kdDebug() << QString( "NewTab: %1, reload: %2" )
820     .arg( args.newTab() ? "yes" : "no" )
821     .arg( args.reload ? "yes" : "no" ) << endl;
822
823     /* open 2ch board */
824     /* if usr1 == 1, open the board with new tab. */
825     if ( mimetype == "kita_open_2chboard" ) {
826         bool withNewTab = ( usr1 == 1 );
827         m_boardDock->slotLoadBoard( url, withNewTab );
828         return ;
829     }
830
831     /* open 2ch thread */
832     /* if usr1 == 1, open the thread with new tab. */
833     if ( mimetype == "kita_open_2chthread" ) {
834         bool withNewTab = ( usr1 == 1 || usr1 == 2 );
835         bool byBackground = ( usr1 == 2 );
836         m_threadDock->slotShowThread( url, withNewTab, byBackground );
837         return ;
838     }
839
840     /*  open url with web browser, etc. */
841     if ( mimetype != QString::null ) {
842         if ( Kita::Config::copyOnClick() ) {
843             QClipboard * clipboard = QApplication::clipboard();
844             clipboard->setText( url.prettyURL(), QClipboard::Clipboard );
845             clipboard->setText( url.prettyURL(), QClipboard::Selection );
846         } else {
847             KRun::runURL( url, mimetype );
848         }
849         return ;
850     }
851
852     /* open thread with new thread tab */
853     if ( Kita::DatManager::isThreadEnrolled( url ) ) {
854         m_threadDock->slotShowThread( url, true );
855         return ;
856     }
857
858     /* bmp file */
859     if ( Kita::ImgManager::isBMP( url ) ) {
860         QMessageBox::warning( this,
861                               "Kita",
862                               i18n( "This is a BMP file. It might contain a Troy." ) );
863         return ;
864     }
865
866
867     /* open with image viewer */
868     /* if usr1 == 0, image viewer is hidden. */
869     /* usrUrl1 is datURL.                    */
870     if ( Kita::Config::useImageViewer() ) {
871         if ( Kita::ImgManager::isImgFile( url, FALSE ) ) {
872
873             if ( !usr1 ) m_imageDock->slotHideDock();
874             m_imageDock->slotOpenURL( url, usrUrl1, usr1 );
875             return ;
876         }
877     }
878
879     if ( mimetype == QString::null ) {
880         mimetype = KIO::NetAccess::mimetype( url , this );
881     }
882     kdDebug() << QString( "queried MIME type: \"%1\"" ).arg( mimetype ) << endl;
883
884     /* open with kpart */
885     if ( Kita::Config::usePart() ) {
886         if ( Kita::Config::partMimeList().find( mimetype ) != Kita::Config::partMimeList().end() ) {
887             KService::Ptr service = KServiceTypeProfile::preferredService( mimetype, "KParts/ReadOnlyPart" );
888             if ( service ) {
889                 QString libName = QFile::encodeName( service->library() );
890
891                 if ( !Kita::Config::useKitaNavi() ) m_threadDock->slotShowPart( url, libName, mimetype );
892                 else m_naviDock ->slotShowPart( url, libName, mimetype );
893                 return ;
894             }
895         }
896     }
897
898     if ( Kita::Config::copyOnClick() ) {
899         QClipboard * clipboard = QApplication::clipboard();
900         clipboard->setText( url.prettyURL(), QClipboard::Clipboard );
901         clipboard->setText( url.prettyURL(), QClipboard::Selection );
902     } else {
903         KRun::runURL( url, mimetype );
904     }
905 }
906
907
908 void KitaMainWindow::loadCache()
909 {
910     KitaThreadInfo * cache = KitaThreadInfo::getInstance();
911     QString cacheConfigPath = locateLocal( "appdata", "cache" );
912     QFile file( cacheConfigPath );
913     if ( file.open( IO_ReadOnly ) ) {
914         QDataStream stream( &file );
915         stream >> *cache;
916     }
917 }
918
919 void KitaMainWindow::saveCache()
920 {
921     KitaThreadInfo * cache = KitaThreadInfo::getInstance();
922     QString cacheConfigPath = locateLocal( "appdata", "cache" );
923     QFile file( cacheConfigPath );
924     if ( file.open( IO_WriteOnly ) ) {
925         QDataStream stream( &file );
926         stream << *cache;
927     }
928 }
929
930
931 /*!
932     \fn KitaMainWindow::setFont(QFont& font)
933  */
934
935 void KitaMainWindow::setFont( const QFont& font )
936 {
937     m_boardTab->setFont( font );
938     m_bbsTab->setFont( font );
939 }
940
941
942 void KitaMainWindow::setUrl( const KURL& url )
943 {
944     m_urlLine->setText( url.url() );
945 }
946
947 void KitaMainWindow::bookmark( const QString& datURL, bool on )
948 {
949     FavoriteThreads * favorite = FavoriteThreads::getInstance();
950
951     if ( on ) {
952         favorite->insert( datURL );
953     } else {
954         favorite->remove( datURL );
955     }
956     saveFavorites();
957     emit favoritesUpdated();
958 }
959
960 void KitaMainWindow::loadFavorites()
961 {
962     QString favoritesConfigPath = locateLocal( "appdata", "favorites.xml" );
963     QFile file( favoritesConfigPath );
964     if ( file.open( IO_ReadOnly ) ) {
965         QTextStream stream( &file );
966         stream.setEncoding( QTextStream::UnicodeUTF8 );
967
968         QString xml = stream.read();
969         FavoriteThreads::readFromXML( xml );
970     }
971 }
972
973 void KitaMainWindow::saveFavorites()
974 {
975     QString favoritesConfigPath = locateLocal( "appdata", "favorites.xml" );
976     QFile file( favoritesConfigPath );
977     if ( file.open( IO_WriteOnly ) ) {
978         QTextStream stream( &file );
979         stream.setEncoding( QTextStream::UnicodeUTF8 );
980         stream << FavoriteThreads::getInstance() ->toXML();
981     }
982 }
983
984 void KitaMainWindow::loadCompletion()
985 {
986     QString configPath = locateLocal( "appdata", "completion" );
987     KSimpleConfig config( configPath );
988
989     Kita::Config::setNameCompletionList( config.readListEntry( "name" ) );
990 }
991
992 void KitaMainWindow::loadStyleSheet()
993 {
994     QString configPath = locateLocal( "appdata", "stylesheet" );
995     QFile file( configPath );
996     if ( file.open( IO_ReadOnly ) ) {
997         QTextStream stream( &file );
998         stream.setEncoding( QTextStream::UnicodeUTF8 );
999
1000         QStringList list;
1001         QString str;
1002
1003         while ( ( str = stream.readLine() ) != QString::null ) {
1004             if ( ! str.isEmpty() ) {
1005                 list << str;
1006             }
1007         }
1008         KitaConfig::setStyleSheetText( list.join( "\n" ) );
1009     }
1010 }
1011
1012 void KitaMainWindow::saveStyleSheet()
1013 {
1014     QString configPath = locateLocal( "appdata", "stylesheet" );
1015     QFile file( configPath );
1016     if ( file.open( IO_WriteOnly ) ) {
1017         QTextStream stream( &file ) ;
1018         stream.setEncoding( QTextStream::UnicodeUTF8 );
1019
1020         stream << KitaConfig::styleSheetText();
1021     }
1022 }
1023
1024 void KitaMainWindow::loadAsciiArt()
1025 {
1026     QString configPath = locateLocal( "appdata", "asciiart" );
1027     QFile file( configPath );
1028     if ( file.open( IO_ReadOnly ) ) {
1029         QTextStream stream( &file );
1030         stream.setEncoding( QTextStream::UnicodeUTF8 );
1031
1032         QStringList list;
1033         QString str;
1034
1035         while ( ( str = stream.readLine() ) != QString::null ) {
1036             if ( ! str.isEmpty() ) {
1037                 list << str;
1038             }
1039         }
1040         Kita::AsciiArtConfig::setAsciiArtList( list );
1041     }
1042 }
1043
1044 void KitaMainWindow::loadFavoriteBoards()
1045 {
1046     QString configPath = locateLocal( "appdata", "favorite_boards.xml" );
1047     QFile file( configPath );
1048     if ( file.open( IO_ReadOnly ) ) {
1049         QTextStream stream( &file );
1050         stream.setEncoding( QTextStream::UnicodeUTF8 );
1051
1052         QString xml = stream.read();
1053         Kita::FavoriteBoards::readFromXML( xml );
1054     }
1055 }
1056
1057 void KitaMainWindow::saveFavoriteBoards()
1058 {
1059     QString configPath = locateLocal( "appdata", "favorite_boards.xml" );
1060     QFile file( configPath );
1061     if ( file.open( IO_WriteOnly ) ) {
1062         QTextStream stream( &file );
1063         stream.setEncoding( QTextStream::UnicodeUTF8 );
1064         stream << Kita::FavoriteBoards::toXML();
1065     }
1066 }
1067
1068 void KitaMainWindow::loadAboneIDList()
1069 {
1070     QString configPath = locateLocal( "appdata", "abone_id" );
1071     QFile file( configPath );
1072     if ( file.open( IO_ReadOnly ) ) {
1073         QTextStream stream( &file );
1074         stream.setEncoding( QTextStream::UnicodeUTF8 );
1075
1076         QStringList list;
1077         QString str;
1078
1079         while ( ( str = stream.readLine() ) != QString::null ) {
1080             if ( ! str.isEmpty() ) {
1081                 list << str;
1082             }
1083         }
1084         Kita::AboneConfig::setAboneIDList( list );
1085     }
1086 }
1087
1088 void KitaMainWindow::loadAboneNameList()
1089 {
1090     QString configPath = locateLocal( "appdata", "abone_name" );
1091     QFile file( configPath );
1092     if ( file.open( IO_ReadOnly ) ) {
1093         QTextStream stream( &file );
1094         stream.setEncoding( QTextStream::UnicodeUTF8 );
1095
1096         QStringList list;
1097         QString str;
1098
1099         while ( ( str = stream.readLine() ) != QString::null ) {
1100             if ( ! str.isEmpty() ) {
1101                 list << str;
1102             }
1103         }
1104         Kita::AboneConfig::setAboneNameList( list );
1105     }
1106 }
1107
1108 void KitaMainWindow::loadAboneWordList()
1109 {
1110     QString configPath = locateLocal( "appdata", "abone_word" );
1111     QFile file( configPath );
1112     if ( file.open( IO_ReadOnly ) ) {
1113         QTextStream stream( &file );
1114         stream.setEncoding( QTextStream::UnicodeUTF8 );
1115
1116         QStringList list;
1117         QString str;
1118
1119         while ( ( str = stream.readLine() ) != QString::null ) {
1120             if ( ! str.isEmpty() ) {
1121                 list << str;
1122             }
1123         }
1124         Kita::AboneConfig::setAboneWordList( list );
1125     }
1126 }
1127
1128 void KitaMainWindow::slotSetMainCaption( const QString& captionStr )
1129 {
1130     setCaption( captionStr );
1131 }
1132
1133 void KitaMainWindow::slotEditCopy()
1134 {
1135     QWidget * widget = kapp->focusWidget();
1136
1137     if ( widget ) {
1138         QKeyEvent e( QEvent::KeyPress, Key_C, 'C', ControlButton );
1139         QApplication::sendEvent( widget, &e );
1140     }
1141 }
1142
1143 void KitaMainWindow::login()
1144 {
1145     if ( Kita::Account::login( Kita::Config::userID(), Kita::Config::password() ) ) {
1146         slotSetMainStatusbar( i18n( "Login succeeded." ) );
1147     } else {
1148         slotSetMainStatusbar( i18n( "Login failed." ) );
1149     }
1150 }
1151
1152 void KitaMainWindow::favoritesEdit()
1153 {
1154     FavoritesWindow * window = new FavoritesWindow( this );
1155     window->show();
1156 }
1157
1158 #include "mainwindow.moc"