korganizer

alarmdockwindow.cpp
1 /*
2  This file is part of KOrganizer.
3 
4  Copyright (c) 2003 Cornelius Schumacher <schumacher@kde.org>
5 
6  This program is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation; either version 2 of the License, or
9  (at your option) any later version.
10 
11  This program is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with this program; if not, write to the Free Software
18  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 
20  As a special exception, permission is given to link this program
21  with any edition of TQt, and distribute the resulting executable,
22  without including the source code for TQt in the source distribution.
23 */
24 
25 #include "alarmdockwindow.h"
26 #include "koalarmclient.h"
27 
28 #include <kapplication.h>
29 #include <kdebug.h>
30 #include <kdeversion.h>
31 #include <klocale.h>
32 #include <kiconloader.h>
33 #include <kconfig.h>
34 #include <kurl.h>
35 #include <kstandarddirs.h>
36 #include <dcopclient.h>
37 #include <kpopupmenu.h>
38 #include <kmessagebox.h>
39 #include <kaction.h>
40 #include <kstdaction.h>
41 
42 #include <tqtooltip.h>
43 #include <tqfile.h>
44 
45 #include <stdlib.h>
46 
47 AlarmDockWindow::AlarmDockWindow( const char *name )
48  : KSystemTray( 0, name )
49 {
50  // Read the autostart status from the config file
51  KConfig *config = kapp->config();
52  config->setGroup("General");
53  bool autostart = config->readBoolEntry( "Autostart", true );
54  bool alarmsEnabled = config->readBoolEntry( "Enabled", true );
55 
56  mName = i18n( "KOrganizer Reminder Daemon" );
57  setCaption( mName );
58 
59  // Set up icons
60  KGlobal::iconLoader()->addAppDir( "korgac" );
61  mPixmapEnabled = loadSizedIcon( "korgac", width() );
62  mPixmapDisabled = loadSizedIcon( "korgac_disabled", width() );
63 
64  setPixmap( alarmsEnabled ? mPixmapEnabled : mPixmapDisabled );
65 
66  // Set up the context menu
67  mSuspendAll = contextMenu()->insertItem( i18n("Suspend All"), this, TQT_SLOT( slotSuspendAll() ) );
68  mDismissAll = contextMenu()->insertItem( i18n("Dismiss All"), this, TQT_SLOT( slotDismissAll() ) );
69  contextMenu()->setItemEnabled( mSuspendAll, false );
70  contextMenu()->setItemEnabled( mDismissAll, false );
71 
72  contextMenu()->insertSeparator();
73  mAlarmsEnabledId = contextMenu()->insertItem( i18n("Reminders Enabled"), this,
74  TQT_SLOT( toggleAlarmsEnabled() ) );
75  mAutostartId = contextMenu()->insertItem( i18n("Start Reminder Daemon at Login"), this,
76  TQT_SLOT( toggleAutostart() ) );
77  contextMenu()->setItemChecked( mAutostartId, autostart );
78  contextMenu()->setItemChecked( mAlarmsEnabledId, alarmsEnabled );
79 
80  // Disable standard quit behaviour. We have to intercept the quit even, if the
81  // main window is hidden.
82  KActionCollection *ac = actionCollection();
83  const char *quitName = KStdAction::name( KStdAction::Quit );
84  KAction *quit = ac->action( quitName );
85  if ( !quit ) {
86  kdDebug(5890) << "No Quit standard action." << endl;
87  } else {
88 #if KDE_IS_VERSION(3,3,90)
89  quit->disconnect( TQT_SIGNAL( activated() ), this,
90  TQT_SLOT( maybeQuit() ) );
91  connect( quit, TQT_SIGNAL( activated() ), TQT_SLOT( slotQuit() ) );
92  }
93 #else //FIXME: remove for KDE 4.0
94  quit->disconnect( TQT_SIGNAL( activated() ), tqApp,
95  TQT_SLOT( closeAllWindows() ) );
96  }
97  connect( this, TQT_SIGNAL( quitSelected() ), TQT_SLOT( slotQuit() ) );
98 #endif
99 
100  TQToolTip::add(this, mName );
101 }
102 
103 AlarmDockWindow::~AlarmDockWindow()
104 {
105 }
106 
107 void AlarmDockWindow::resizeEvent ( TQResizeEvent * )
108 {
109  // Honor Free Desktop specifications that allow for arbitrary system tray icon sizes
110  mPixmapEnabled = loadSizedIcon( "korgac", width() );
111  mPixmapDisabled = loadSizedIcon( "korgac_disabled", width() );
112 
113  KConfig *config = kapp->config();
114  bool alarmsEnabled = config->readBoolEntry( "Enabled", true );
115  setPixmap( alarmsEnabled ? mPixmapEnabled : mPixmapDisabled );
116 }
117 
118 void AlarmDockWindow::slotUpdate( int reminders )
119 {
120  TQToolTip::remove( this );
121  if ( reminders > 0 )
122  {
123  TQToolTip::add( this, i18n( "There is 1 active reminder.",
124  "There are %n active reminders.", reminders ) );
125  contextMenu()->setItemEnabled( mSuspendAll, true );
126  contextMenu()->setItemEnabled( mDismissAll, true );
127  }
128  else
129  {
130  TQToolTip::add( this, mName );
131  contextMenu()->setItemEnabled( mSuspendAll, false );
132  contextMenu()->setItemEnabled( mDismissAll, false );
133  }
134 }
135 
136 void AlarmDockWindow::toggleAlarmsEnabled()
137 {
138  kdDebug(5890) << "AlarmDockWindow::toggleAlarmsEnabled()" << endl;
139 
140  KConfig *config = kapp->config();
141  config->setGroup( "General" );
142 
143  bool enabled = !contextMenu()->isItemChecked( mAlarmsEnabledId );
144  contextMenu()->setItemChecked( mAlarmsEnabledId, enabled );
145  setPixmap( enabled ? mPixmapEnabled : mPixmapDisabled );
146 
147  config->writeEntry( "Enabled", enabled );
148  config->sync();
149 }
150 
151 void AlarmDockWindow::toggleAutostart()
152 {
153  bool autostart = !contextMenu()->isItemChecked( mAutostartId );
154 
155  enableAutostart( autostart );
156 }
157 
158 void AlarmDockWindow::slotSuspendAll()
159 {
160  emit suspendAllSignal();
161 }
162 
163 void AlarmDockWindow::slotDismissAll()
164 {
165  emit dismissAllSignal();
166 }
167 
168 void AlarmDockWindow::enableAutostart( bool enable )
169 {
170  KConfig *config = kapp->config();
171  config->setGroup( "General" );
172  config->writeEntry( "Autostart", enable );
173  config->sync();
174 
175  contextMenu()->setItemChecked( mAutostartId, enable );
176 }
177 
178 void AlarmDockWindow::mousePressEvent( TQMouseEvent *e )
179 {
180  if ( e->button() == Qt::LeftButton ) {
181  kapp->startServiceByDesktopName( "korganizer", TQString() );
182  } else {
183  KSystemTray::mousePressEvent( e );
184  }
185 }
186 
187 //void AlarmDockWindow::closeEvent( TQCloseEvent * )
188 void AlarmDockWindow::slotQuit()
189 {
190  int result = KMessageBox::questionYesNoCancel( this,
191  i18n("Do you want to start the KOrganizer reminder daemon at login "
192  "(note that you will not get reminders whilst the daemon is not running)?"),
193  i18n("Close KOrganizer Reminder Daemon"),
194  i18n("Start"), i18n("Do Not Start"),
195  TQString::fromLatin1("AskForStartAtLogin")
196  );
197 
198  bool autostart = true;
199  if ( result == KMessageBox::No ) autostart = false;
200  enableAutostart( autostart );
201 
202  if ( result != KMessageBox::Cancel )
203  emit quitSignal();
204 }
205 
206 #include "alarmdockwindow.moc"