korganizer

alarmdockwindow.cpp
00001 /*
00002     This file is part of KOrganizer.
00003 
00004     Copyright (c) 2003 Cornelius Schumacher <schumacher@kde.org>
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 TQt, and distribute the resulting executable,
00022     without including the source code for TQt in the source distribution.
00023 */
00024 
00025 #include "alarmdockwindow.h"
00026 #include "koalarmclient.h"
00027 
00028 #include <tdeapplication.h>
00029 #include <kdebug.h>
00030 #include <tdeversion.h>
00031 #include <tdelocale.h>
00032 #include <kiconloader.h>
00033 #include <tdeconfig.h>
00034 #include <kurl.h>
00035 #include <kstandarddirs.h>
00036 #include <dcopclient.h>
00037 #include <tdepopupmenu.h>
00038 #include <tdemessagebox.h>
00039 #include <tdeaction.h>
00040 #include <kstdaction.h>
00041 
00042 #include <tqtooltip.h>
00043 #include <tqfile.h>
00044 
00045 #include <stdlib.h>
00046 
00047 AlarmDockWindow::AlarmDockWindow( const char *name )
00048   : KSystemTray( 0, name )
00049 {
00050   // Read the autostart status from the config file
00051   TDEConfig *config = kapp->config();
00052   config->setGroup("General");
00053   bool autostart = config->readBoolEntry( "Autostart", true );
00054   bool alarmsEnabled = config->readBoolEntry( "Enabled", true );
00055 
00056   mName = i18n( "KOrganizer Reminder Daemon" );
00057   setCaption( mName );
00058 
00059   // Set up icons
00060   TDEGlobal::iconLoader()->addAppDir( "korgac" );
00061   mPixmapEnabled  = loadIcon( "korgac" );
00062   mPixmapDisabled = loadIcon( "korgac_disabled" );
00063 
00064   setPixmap( alarmsEnabled ? mPixmapEnabled : mPixmapDisabled );
00065 
00066   // Set up the context menu
00067   mSuspendAll = contextMenu()->insertItem( i18n("Suspend All"), this, TQT_SLOT( slotSuspendAll() ) );
00068   mDismissAll = contextMenu()->insertItem( i18n("Dismiss All"), this, TQT_SLOT( slotDismissAll() ) );
00069   contextMenu()->setItemEnabled( mSuspendAll, false );
00070   contextMenu()->setItemEnabled( mDismissAll, false );
00071 
00072   contextMenu()->insertSeparator();
00073   mAlarmsEnabledId = contextMenu()->insertItem( i18n("Reminders Enabled"), this,
00074                                                 TQT_SLOT( toggleAlarmsEnabled() ) );
00075   mAutostartId = contextMenu()->insertItem( i18n("Start Reminder Daemon at Login"), this,
00076                                                 TQT_SLOT( toggleAutostart() ) );
00077   contextMenu()->setItemChecked( mAutostartId, autostart );
00078   contextMenu()->setItemChecked( mAlarmsEnabledId, alarmsEnabled );
00079 
00080   // Disable standard quit behaviour. We have to intercept the quit even, if the
00081   // main window is hidden.
00082   TDEActionCollection *ac = actionCollection();
00083   const char *quitName = KStdAction::name( KStdAction::Quit );
00084   TDEAction *quit = ac->action( quitName );
00085   if ( !quit ) {
00086     kdDebug(5890) << "No Quit standard action." << endl;
00087   } else {
00088 #if KDE_IS_VERSION(3,3,90)
00089     quit->disconnect( TQT_SIGNAL( activated() ), this,
00090                       TQT_SLOT( maybeQuit() ) );
00091     connect( quit, TQT_SIGNAL( activated() ), TQT_SLOT( slotQuit() ) );
00092   }
00093 #else //FIXME: remove for KDE 4.0
00094     quit->disconnect( TQT_SIGNAL( activated() ), tqApp,
00095                       TQT_SLOT( closeAllWindows() ) );
00096   }
00097   connect( this, TQT_SIGNAL( quitSelected() ), TQT_SLOT( slotQuit() ) );
00098 #endif
00099 
00100   TQToolTip::add(this, mName );
00101 }
00102 
00103 AlarmDockWindow::~AlarmDockWindow()
00104 {
00105 }
00106 
00107 void AlarmDockWindow::resizeTrayIcon ()
00108 {
00109     // Honor Free Desktop specifications that allow for arbitrary system tray icon sizes
00110     mPixmapEnabled  = loadSizedIcon( "korgac", width() );
00111     mPixmapDisabled = loadSizedIcon( "korgac_disabled", width() );
00112 
00113     TDEConfig *config = kapp->config();
00114     bool alarmsEnabled = config->readBoolEntry( "Enabled", true );
00115     setPixmap( alarmsEnabled ? mPixmapEnabled : mPixmapDisabled );
00116 }
00117 
00118 void AlarmDockWindow::resizeEvent ( TQResizeEvent * )
00119 {
00120     resizeTrayIcon();
00121 }
00122 
00123 void AlarmDockWindow::showEvent ( TQShowEvent *se )
00124 {
00125     resizeTrayIcon();
00126     KSystemTray::showEvent(se);
00127 }
00128 
00129 void AlarmDockWindow::slotUpdate( int reminders )
00130 {
00131   TQToolTip::remove( this );
00132   if ( reminders > 0 )
00133   {
00134     TQToolTip::add( this, i18n( "There is 1 active reminder.",
00135                    "There are %n active reminders.", reminders ) );
00136     contextMenu()->setItemEnabled( mSuspendAll, true );
00137     contextMenu()->setItemEnabled( mDismissAll, true );
00138   }
00139   else
00140   {
00141     TQToolTip::add( this, mName );
00142     contextMenu()->setItemEnabled( mSuspendAll, false );
00143     contextMenu()->setItemEnabled( mDismissAll, false );
00144   }
00145 }
00146 
00147 void AlarmDockWindow::toggleAlarmsEnabled()
00148 {
00149   kdDebug(5890) << "AlarmDockWindow::toggleAlarmsEnabled()" << endl;
00150 
00151   TDEConfig *config = kapp->config();
00152   config->setGroup( "General" );
00153 
00154   bool enabled = !contextMenu()->isItemChecked( mAlarmsEnabledId );
00155   contextMenu()->setItemChecked( mAlarmsEnabledId, enabled );
00156   setPixmap( enabled ? mPixmapEnabled : mPixmapDisabled );
00157 
00158   config->writeEntry( "Enabled", enabled );
00159   config->sync();
00160 }
00161 
00162 void AlarmDockWindow::toggleAutostart()
00163 {
00164   bool autostart = !contextMenu()->isItemChecked( mAutostartId );
00165 
00166   enableAutostart( autostart );
00167 }
00168 
00169 void AlarmDockWindow::slotSuspendAll()
00170 {
00171   emit suspendAllSignal();
00172 }
00173 
00174 void AlarmDockWindow::slotDismissAll()
00175 {
00176   emit dismissAllSignal();
00177 }
00178 
00179 void AlarmDockWindow::enableAutostart( bool enable )
00180 {
00181   TDEConfig *config = kapp->config();
00182   config->setGroup( "General" );
00183   config->writeEntry( "Autostart", enable );
00184   config->sync();
00185 
00186   contextMenu()->setItemChecked( mAutostartId, enable );
00187 }
00188 
00189 void AlarmDockWindow::mousePressEvent( TQMouseEvent *e )
00190 {
00191   if ( e->button() == Qt::LeftButton ) {
00192     kapp->startServiceByDesktopName( "korganizer", TQString() );
00193   } else {
00194     KSystemTray::mousePressEvent( e );
00195   }
00196 }
00197 
00198 //void AlarmDockWindow::closeEvent( TQCloseEvent * )
00199 void AlarmDockWindow::slotQuit()
00200 {
00201   int result = KMessageBox::questionYesNoCancel( this,
00202       i18n("Do you want to start the KOrganizer reminder daemon at login "
00203            "(note that you will not get reminders whilst the daemon is not running)?"),
00204       i18n("Close KOrganizer Reminder Daemon"),
00205       i18n("Start"), i18n("Do Not Start"),
00206       TQString::fromLatin1("AskForStartAtLogin")
00207       );
00208 
00209   bool autostart = true;
00210   if ( result == KMessageBox::No ) autostart = false;
00211   enableAutostart( autostart );
00212 
00213   if ( result != KMessageBox::Cancel )
00214     emit quitSignal();
00215 }
00216 
00217 #include "alarmdockwindow.moc"