akregator/src

mainwindow.cpp

00001 /*
00002     This file is part of Akregator.
00003 
00004     Copyright (C) 2004 Stanislav Karchebny <Stanislav.Karchebny@kdemail.net>
00005 
00006     This program is free software; you can redistribute it and/or modify
00007     it under the terms of the GNU General Public License as published by
00008     the Free Software Foundation; either version 2 of the License, or
00009     (at your option) any later version.
00010 
00011     This program is distributed in the hope that it will be useful,
00012     but WITHOUT ANY WARRANTY; without even the implied warranty of
00013     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00014     GNU General Public License for more details.
00015 
00016     You should have received a copy of the GNU General Public License
00017     along with this program; if not, write to the Free Software
00018     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00019 
00020     As a special exception, permission is given to link this program
00021     with any edition of Qt, and distribute the resulting executable,
00022     without including the source code for Qt in the source distribution.
00023 */
00024 
00025 #include "mainwindow.h"
00026 #include "akregator_part.h"
00027 #include "akregatorconfig.h"
00028 
00029 //settings
00030 
00031 #include <dcopclient.h>
00032 
00033 #include <kaction.h>
00034 #include <kapplication.h>
00035 #include <kconfig.h>
00036 #include <kdebug.h>
00037 #include <kedittoolbar.h>
00038 #include <kfiledialog.h>
00039 #include <kglobal.h>
00040 #include <kkeydialog.h>
00041 #include <klibloader.h>
00042 #include <klocale.h>
00043 #include <kmessagebox.h>
00044 #include <kparts/partmanager.h>
00045 #include <ksqueezedtextlabel.h>
00046 #include <kstandarddirs.h>
00047 #include <kstatusbar.h>
00048 #include <kstdaction.h>
00049 #include <kurl.h>
00050 
00051 #include "progressdialog.h"
00052 #include "statusbarprogresswidget.h"
00053 #include "trayicon.h"
00054 
00055 #include <qmetaobject.h>
00056 #include <qpen.h>
00057 #include <qpainter.h>
00058 #include <private/qucomextra_p.h>
00059 #include <qtimer.h>
00060 
00061 
00062 namespace Akregator {
00063 
00064 BrowserInterface::BrowserInterface( MainWindow *shell, const char *name )
00065     : KParts::BrowserInterface( shell, name )
00066 {
00067     m_shell = shell;
00068 }
00069 
00070 MainWindow::MainWindow()
00071     : KParts::MainWindow( 0L, "akregator_mainwindow" ){
00072     // set the shell's ui resource file
00073     setXMLFile("akregator_shell.rc");
00074 
00075     m_browserIface=new BrowserInterface(this, "browser_interface");
00076 
00077     m_part=0;
00078 
00079     // then, setup our actions
00080 
00081     toolBar()->show();
00082     // and a status bar
00083     statusBar()->show();
00084 
00085     int statH=fontMetrics().height()+2;
00086     m_statusLabel = new KSqueezedTextLabel(this);
00087     m_statusLabel->setTextFormat(Qt::RichText);
00088     m_statusLabel->setSizePolicy(QSizePolicy( QSizePolicy::Ignored, QSizePolicy::Fixed ));
00089     m_statusLabel->setMinimumWidth( 0 );
00090     m_statusLabel->setFixedHeight( statH );
00091     statusBar()->addWidget (m_statusLabel, 1, false);
00092 
00093     setupActions();
00094     createGUI(0L);
00095 }
00096 
00097 bool MainWindow::loadPart()
00098 {
00099     // this routine will find and load our Part.  it finds the Part by
00100     // name which is a bad idea usually.. but it's alright in this
00101     // case since our Part is made for this Shell
00102     KLibFactory *factory = KLibLoader::self()->factory("libakregatorpart");
00103     if (factory)
00104     {
00105         // now that the Part is loaded, we cast it to a Part to get
00106         // our hands on it
00107         m_part = static_cast<Akregator::Part*>(factory->create(this, "akregator_part", "KParts::ReadOnlyPart" ));
00108 
00109         if (m_part)
00110         {
00111             // tell the KParts::MainWindow that this is indeed the main widget
00112             setCentralWidget(m_part->widget());
00113 
00114             connect(m_part, SIGNAL(setWindowCaption (const QString &)), this, SLOT(setCaption (const QString &)));
00115 
00116             connect(TrayIcon::getInstance(), SIGNAL(quitSelected()), this, SLOT(slotQuit()));
00117             // and integrate the part's GUI with the shell's
00118             connectActionCollection(m_part->actionCollection());
00119             createGUI(m_part);
00120             browserExtension(m_part)->setBrowserInterface(m_browserIface);
00121             setAutoSaveSettings();
00122             return true;
00123         }
00124         return false;
00125     }
00126     else
00127     {
00128         KMessageBox::error(this, i18n("Could not find the Akregator part; please check your installation."));
00129         return false;
00130     }
00131 
00132 }
00133 
00134 void MainWindow::setupProgressWidgets()
00135 {
00136     KPIM::ProgressDialog *progressDialog = new KPIM::ProgressDialog( statusBar(), this );
00137     progressDialog->raise();
00138     progressDialog->hide();
00139     m_progressBar = new KPIM::StatusbarProgressWidget( progressDialog, statusBar() );
00140     m_progressBar->show();
00141     statusBar()->addWidget( m_progressBar, 0, true );
00142 }
00143 
00144 MainWindow::~MainWindow()
00145 {
00146 }
00147 
00148 void MainWindow::setCaption(const QString &a)
00149 {
00150     KParts::MainWindow::setCaption(a);
00151 }
00152 
00153 void MainWindow::setupActions()
00154 {
00155     connectActionCollection(actionCollection());
00156 
00157     KStdAction::quit(kapp, SLOT(quit()), actionCollection());
00158 
00159     setStandardToolBarMenuEnabled(true);
00160     createStandardStatusBarAction();
00161 
00162     KStdAction::keyBindings(this, SLOT(optionsConfigureKeys()), actionCollection());
00163     KStdAction::configureToolbars(this, SLOT(optionsConfigureToolbars()), actionCollection());
00164 }
00165 
00166 void MainWindow::saveProperties(KConfig* config)
00167 {
00168     if (!m_part)
00169         loadPart();
00170 
00171     static_cast<Akregator::Part*>(m_part)->saveProperties(config);
00172     config->writeEntry("docked", isHidden());
00173 
00174     //delete m_part;
00175 }
00176 
00177 void MainWindow::readProperties(KConfig* config)
00178 {
00179     if (!m_part)
00180         loadPart();
00181     static_cast<Akregator::Part*>(m_part)->readProperties(config);
00182     
00183     if (Settings::showTrayIcon() && config->readBoolEntry("docked", false)) 
00184         hide();
00185     else
00186         show();
00187 }
00188 
00189 void MainWindow::optionsConfigureKeys()
00190 {
00191     KKeyDialog dlg( true, this );
00192 
00193     dlg.insert(actionCollection());
00194     if (m_part)
00195         dlg.insert(m_part->actionCollection());
00196 
00197     dlg.configure();
00198 }
00199 
00200 void MainWindow::optionsConfigureToolbars()
00201 {
00202     saveMainWindowSettings(KGlobal::config(), autoSaveGroup());
00203 
00204     // use the standard toolbar editor
00205     KEditToolbar dlg(factory());
00206     connect(&dlg, SIGNAL(newToolbarConfig()),
00207             this, SLOT(applyNewToolbarConfig()));
00208     dlg.exec();
00209 }
00210 
00211 
00212 
00213 void MainWindow::applyNewToolbarConfig()
00214 {
00215     applyMainWindowSettings(KGlobal::config(), autoSaveGroup());
00216 }
00217 
00218 
00219 KParts::BrowserExtension *MainWindow::browserExtension(KParts::ReadOnlyPart *p)
00220 {
00221     return KParts::BrowserExtension::childObject( p );
00222 }
00223 
00224 
00225 // from konqmainwindow
00226 void MainWindow::connectActionCollection( KActionCollection *coll )
00227 {
00228     if (!coll) return;
00229     connect( coll, SIGNAL( actionStatusText( const QString & ) ),
00230               m_statusLabel, SLOT( setText( const QString & ) ) );
00231     connect( coll, SIGNAL( clearStatusText() ),
00232              this, SLOT( slotClearStatusText() ) );
00233 }
00234 
00235 bool MainWindow::queryExit()
00236 {
00237     kdDebug() << "MainWindow::queryExit()" << endl;
00238     if ( !kapp->sessionSaving() )
00239     {
00240         delete m_part; // delete that here instead of dtor to ensure nested khtmlparts are deleted before singleton objects like KHTMLPageCache
00241         m_part = 0;
00242     }
00243     else
00244         kdDebug("MainWindow::queryExit(): saving session");
00245 
00246     return KMainWindow::queryExit();
00247 }
00248 
00249 void MainWindow::slotQuit()
00250 {
00251     if (TrayIcon::getInstance())
00252         TrayIcon::getInstance()->hide();
00253     kapp->quit();
00254 }
00255 
00256 bool MainWindow::queryClose()
00257 {
00258     if (kapp->sessionSaving() || TrayIcon::getInstance() == 0 || TrayIcon::getInstance()->isHidden() )
00259     {
00260         return true;
00261     }
00262     else
00263     {
00264         QPixmap shot = TrayIcon::getInstance()->takeScreenshot();
00265 
00266         // Associate source to image and show the dialog:
00267         QMimeSourceFactory::defaultFactory()->setPixmap("systray_shot", shot);
00268         KMessageBox::information(this, i18n( "<qt><p>Closing the main window will keep Akregator running in the system tray. Use 'Quit' from the 'File' menu to quit the application.</p><p><center><img source=\"systray_shot\"></center></p></qt>" ), i18n( "Docking in System Tray" ), "hideOnCloseInfo");
00269         hide();
00270         return false;
00271     }
00272 }
00273 
00274 
00275 void MainWindow::slotClearStatusText()
00276 {
00277     m_statusLabel->setText(QString());
00278 }
00279 
00280 void MainWindow::slotSetStatusBarText( const QString & text )
00281 {
00282     m_statusLabel->setText(text);
00283 }
00284 
00285 } // namespace Akregator
00286 
00287 #include "mainwindow.moc"
00288 
00289 
00290 // vim: set et ts=4 sts=4 sw=4:
KDE Home | KDE Accessibility Home | Description of Access Keys