korganizer Library API Documentation

koapp.cpp

00001 /*
00002     This file is part of KOrganizer.
00003     Copyright (c) 1999 Preston Brown
00004     Copyright (c) 2000,2001 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., 675 Mass Ave, Cambridge, MA 02139, 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 <stdio.h>
00026 #include <stdlib.h>
00027 
00028 #include <qfile.h>
00029 
00030 #include <kstandarddirs.h>
00031 #include <kglobal.h>
00032 #include <kcmdlineargs.h>
00033 #include <kconfig.h>
00034 #include <kdebug.h>
00035 #include <dcopclient.h>
00036 #include <kwin.h>
00037 #include <kurl.h>
00038 #include <kprocess.h>
00039 
00040 #include <libkcal/calendarlocal.h>
00041 #include <libkcal/filestorage.h>
00042 #include <libkcal/calformat.h>
00043 
00044 #include "kalarmd/alarmdaemoniface_stub.h"
00045 
00046 #include "korganizer.h"
00047 #include "koprefs.h"
00048 #include "version.h"
00049 
00050 #include "koapp.h"
00051 #include "koapp.moc"
00052 
00053 KOrganizerApp::KOrganizerApp() : KUniqueApplication()
00054 {
00055   QString prodId = "-//K Desktop Environment//NONSGML KOrganizer %1//EN";
00056   CalFormat::setApplication( "KOrganizer", prodId.arg( korgVersion ) );
00057 }
00058 
00059 KOrganizerApp::~KOrganizerApp()
00060 {
00061 }
00062 
00063 void KOrganizerApp::displayImminent( const KURL &url, int numdays )
00064 {
00065   if (!url.isLocalFile()) {
00066     printf("%s", i18n("Unable to handle remote calendar.\n").local8Bit().data());
00067     return;
00068   }
00069 
00070   Calendar *cal = new CalendarLocal(KOPrefs::instance()->mTimeZoneId.local8Bit());
00071 
00072   QDate currDate(QDate::currentDate());
00073   Event *currEvent;
00074 
00075   FileStorage storage( cal, url.path() );
00076 
00077   if ( !storage.load() ) {
00078     printf("%s", i18n("Could not load calendar '%1'.\n").arg(url.path()).local8Bit().data());
00079     exit(0);
00080   }
00081 
00082   for (int i = 1; i <= numdays; i++) {
00083     printf("%s\n",(const char *)KGlobal::locale()->formatDate(currDate).local8Bit());
00084 
00085     QPtrList<Event> tmpList( cal->events( currDate, true ) );
00086     printf("---------------------------------------------------------------\n");
00087     if (tmpList.count() > 0) {
00088       for (currEvent = tmpList.first(); currEvent; currEvent = tmpList.next()) {
00089         printf("%s",(const char *)currEvent->summary().local8Bit());
00090         if (!currEvent->doesFloat()) {
00091           printf(" (%s - %s)",(const char *)currEvent->dtStartStr().local8Bit(),
00092                  (const char *)currEvent->dtEndStr().local8Bit());
00093         }
00094         printf("\n");
00095       }
00096     } else {
00097       printf("%s", i18n("(no events)\n").local8Bit().data());
00098     }
00099 
00100     printf("---------------------------------------------------------------\n");
00101     QPtrList<Todo> tmpList2 = cal->todos(currDate);
00102     Todo *currTodo;
00103     if (tmpList.count() > 0) {
00104       for (currTodo = tmpList2.first(); currTodo; currTodo = tmpList2.next()) {
00105         printf("%s",(const char *)currTodo->summary().local8Bit());
00106         if (!currTodo->doesFloat()) {
00107           printf(" (%s)",(const char *)currTodo->dtDueStr().local8Bit());
00108         }
00109         printf("\n");
00110       }
00111     } else {
00112       printf("%s", i18n("(no todos)\n").local8Bit().data());
00113     }
00114 
00115     printf("\n");
00116     currDate = currDate.addDays(1);
00117   }
00118 }
00119 
00120 
00121 void KOrganizerApp::startAlarmDaemon()
00122 {
00123   kdDebug() << "Starting alarm daemon" << endl;
00124 
00125   // Start alarmdaemon. It is a KUniqueApplication, that means it is
00126   // automatically made sure that there is only one instance of the alarm daemon
00127   // running.
00128   QString execStr = locate("exe","kalarmd");
00129   system(QFile::encodeName(execStr));
00130 
00131   kdDebug() << "Starting alarm daemon done" << endl;
00132 }
00133 
00134 void KOrganizerApp::startAlarmClient()
00135 {
00136   kdDebug() << "Starting alarm client" << endl;
00137 
00138   KProcess *proc = new KProcess;
00139   *proc << "korgac";
00140   *proc << "--miniicon" <<  "korganizer";
00141   connect( proc, SIGNAL( processExited( KProcess * ) ),
00142            SLOT( startCompleted( KProcess * ) ) );
00143   if (!proc->start())
00144       delete proc;
00145 }
00146 
00147 void KOrganizerApp::startCompleted( KProcess *process )
00148 {
00149   delete process;
00150 }
00151 
00152 int KOrganizerApp::newInstance()
00153 {
00154   kdDebug() << "KOApp::newInstance()" << endl;
00155 
00156   KCmdLineArgs *args = KCmdLineArgs::parsedArgs();
00157   
00158   // process command line options
00159   int numDays = 0;
00160   if (args->isSet("list")) {
00161     numDays = 1;
00162   } else if (args->isSet("show")) {
00163     numDays = args->getOption("show").toInt();
00164   } else {
00165     if (!dcopClient()->isApplicationRegistered("kalarmd")) {
00166       startAlarmDaemon();
00167     }
00168     if (!dcopClient()->isApplicationRegistered("korgac")) {
00169       startAlarmClient();
00170     }
00171 
00172     kdDebug() << "KOApp::newInstance() registerApp" << endl;
00173     // Register this application with the alarm daemon
00174     AlarmDaemonIface_stub stub( "kalarmd", "ad" );
00175     stub.registerApp( "korgac", "KOrganizer", "ac", 3, true );
00176     if( !stub.ok() ) {
00177       kdDebug() << "KOrganizerApp::newInstance(): dcop send failed" << endl;
00178     }
00179   }
00180 
00181   // If filenames was given as argument load this as calendars, one per window.
00182   if (args->count() > 0) {
00183     int i;
00184     for(i=0;i<args->count();++i) {
00185       processCalendar( args->url(i), numDays ); 
00186     }
00187   } else {
00188     KGlobal::config()->setGroup("General");
00189     QString urlString = KGlobal::config()->readEntry("Active Calendar");
00190 
00191     // Force alarm daemon to load active calendar
00192     AlarmDaemonIface_stub stub( "kalarmd", "ad" );
00193     stub.addCal( "korgac", urlString );
00194 
00195     processCalendar( urlString, numDays ); 
00196   }
00197   
00198   kdDebug() << "KOApp::newInstance() done" << endl;
00199   return 0;
00200 }
00201 
00202 
00203 void KOrganizerApp::processCalendar( const KURL &url, int numDays )
00204 {
00205   if (numDays > 0) {
00206     displayImminent( url, numDays );
00207   } else {
00208     if (isRestored()) {
00209       RESTORE(KOrganizer)
00210     } else {
00211       KOrganizer *korg=KOrganizer::findInstance(url);
00212       if (0 == korg) {
00213         korg = new KOrganizer( "KOrganizer MainWindow" ); 
00214         korg->show();
00215         
00216         kdDebug() << "KOrganizerApp::processCalendar(): " << url.url() << endl;
00217         
00218         if (!url.isEmpty()) {
00219           korg->openURL(url);
00220         }
00221       } else
00222           KWin::setActiveWindow(korg->winId());
00223     }
00224   }
00225 }
KDE Logo
This file is part of the documentation for kdelibs Version 3.1.4.
Documentation copyright © 1996-2002 the KDE developers.
Generated on Sat Oct 18 02:47:31 2003 by doxygen 1.3.4 written by Dimitri van Heesch, © 1997-2001