kalarmd Library API Documentation

adconfigdatabase.cpp

00001 /*
00002     Alarm Daemon client data file access.
00003 
00004     This file is part of the KDE alarm daemon.
00005     Copyright (c) 2001 David Jarvie <software@astrojar.org.uk>
00006 
00007     This program is free software; you can redistribute it and/or modify
00008     it under the terms of the GNU General Public License as published by
00009     the Free Software Foundation; either version 2 of the License, or
00010     (at your option) any later version.
00011 
00012     This program is distributed in the hope that it will be useful,
00013     but WITHOUT ANY WARRANTY; without even the implied warranty of
00014     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00015     GNU General Public License for more details.
00016 
00017     You should have received a copy of the GNU General Public License
00018     along with this program; if not, write to the Free Software
00019     Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00020 
00021     As a special exception, permission is given to link this program
00022     with any edition of Qt, and distribute the resulting executable,
00023     without including the source code for Qt in the source distribution.
00024 */
00025 
00026 #include <kstandarddirs.h>
00027 #include <kurl.h>
00028 #include <kdebug.h>
00029 #include <ksimpleconfig.h>
00030 
00031 #include "adconfigdatabase.h"
00032 
00033 // The config file containing client and calendar information
00034 #define CLIENT_DATA_FILE "clients"
00035 
00036 // Config file key strings
00037 const QCString ADConfigDataBase::CLIENT_KEY("Client_");
00038 const QString ADConfigDataBase::CLIENTS_KEY("Clients");
00039 const QCString ADConfigDataBase::GUI_KEY("Gui_");
00040 const QString ADConfigDataBase::GUIS_KEY("Guis");
00041 // Client data file key strings
00042 const QString ADConfigDataBase::CLIENT_CALENDAR_KEY("Calendar");
00043 const QString ADConfigDataBase::CLIENT_TITLE_KEY("Title");
00044 const QString ADConfigDataBase::CLIENT_DCOP_OBJECT_KEY("DCOP object");
00045 const QString ADConfigDataBase::CLIENT_NOTIFICATION_KEY("Notification");
00046 const QString ADConfigDataBase::CLIENT_DISP_CAL_KEY("Display calendar names");
00047 
00048 
00049 ADConfigDataBase::ADConfigDataBase(bool daemon)
00050   : mIsAlarmDaemon(daemon)
00051 {
00052   mCalendars.setAutoDelete(true);
00053 }
00054 
00055 /*
00056  * Read all client applications from the client data file and store them in the client list.
00057  * Open all calendar files listed in the client data file and start monitoring them.
00058  * Calendar files are monitored until their client application registers, upon which
00059  * monitoring ceases until the client application tell the daemon to monitor it.
00060  * Reply = updated Clients option in main Alarm Daemon config file.
00061  */
00062 QString ADConfigDataBase::readConfigData(bool sessionStarting, bool& deletedClients, bool& deletedCalendars,
00063                                          ADCalendarBaseFactory *calFactory)
00064 {
00065   kdDebug(5900) << "ADConfigDataBase::readConfigData()" << endl;
00066   deletedClients   = false;
00067   deletedCalendars = false;
00068   if (mClientDataFile.isEmpty())
00069   {
00070     if (mIsAlarmDaemon)
00071       mClientDataFile = locateLocal("appdata", QString(CLIENT_DATA_FILE));
00072     else
00073       mClientDataFile = locate("data", QString("kalarmd/" CLIENT_DATA_FILE));
00074   }
00075   KSimpleConfig clientConfig(mClientDataFile);
00076   clientConfig.setGroup("General");
00077   QStrList clients;
00078   clientConfig.readListEntry(CLIENTS_KEY, clients);
00079 
00080   // Delete any clients which are no longer in the config file
00081   for (ClientList::Iterator cl = mClients.begin();  cl != mClients.end();  )
00082   {
00083     bool found = false;
00084     for (unsigned int i = 0;  i < clients.count();  ++i)
00085       if (clients.at(i) == (*cl).appName)
00086       {
00087         found = true;
00088         break;
00089       }
00090     if (!found)
00091     {
00092       // This client has disappeared. Remove it and its calendars
00093       for (ADCalendarBase* cal = mCalendars.first();  cal;  cal = mCalendars.next())
00094       {
00095         if (cal->appName() == (*cl).appName)
00096         {
00097           mCalendars.remove(cal);
00098           deletedCalendars = true;
00099         }
00100       }
00101       ClientList::Iterator c = cl;
00102       ++cl;                      // prevent iterator becoming invalid with remove()
00103       mClients.remove(c);
00104       deletedClients = true;
00105     }
00106     else
00107       ++cl;
00108   }
00109 
00110   // Update the clients and calendars lists with the new data
00111   bool writeNewClients = false;
00112   QString newClients;
00113   for (unsigned int i = 0;  i < clients.count();  ++i)
00114   {
00115     kdDebug(5900) << "ADConfigDataBase::readConfigData(): client: "
00116                   << clients.at(i) << endl;
00117     QCString client = clients.at(i);
00118     if ( client.isEmpty() ||
00119          KStandardDirs::findExe( client ).isNull() )
00120     {
00121       // Null client name, or application doesn't exist
00122       if (mIsAlarmDaemon)
00123       {
00124         if (!client.isEmpty())
00125           clientConfig.deleteGroup(CLIENT_KEY + client, true);
00126         writeNewClients = true;
00127       }
00128     }
00129     else
00130     {
00131       QString groupKey = CLIENT_KEY + client;
00132 
00133       // Get this client's details from its own config section.
00134       // If the client is already known, update its details.
00135       ClientInfo info = getClientInfo( client );
00136       if ( info.isValid() )
00137         removeClientInfo( client );
00138       clientConfig.setGroup(groupKey);
00139       QString  title      = clientConfig.readEntry(CLIENT_TITLE_KEY, client);   // read app title (default = app name)
00140       QCString dcopObject = clientConfig.readEntry(CLIENT_DCOP_OBJECT_KEY).local8Bit();
00141       int      type       = clientConfig.readNumEntry(CLIENT_NOTIFICATION_KEY, 0);
00142       bool displayCalName = clientConfig.readBoolEntry(CLIENT_DISP_CAL_KEY, true);
00143       info = ClientInfo( client, title, dcopObject, type, displayCalName, sessionStarting );
00144       mClients.append( info );
00145 
00146       // Get the client's calendar files
00147       QStrList newCalendars;       // to contain a list of calendars configured for this client
00148       int len = CLIENT_CALENDAR_KEY.length();
00149       QMap<QString, QString> entries = clientConfig.entryMap(groupKey);
00150       for (QMap<QString, QString>::ConstIterator it = entries.begin();  it != entries.end();  ++it)
00151       {
00152         if (it.key().startsWith(CLIENT_CALENDAR_KEY))
00153         {
00154           kdDebug(5900) << "ADConfigDataBase::readConfigData(): " << it.key() << "=" << it.data() << endl;
00155           bool ok;
00156           int rcIndex = it.key().mid(len).toInt(&ok);
00157           if (ok)
00158           {
00159             // The config file key is CalendarN, so open the calendar file
00160             int comma1 = it.data().find(',');
00161             if (comma1 >= 0)
00162             {
00163               QDateTime dateTime;    // default to invalid
00164               int comma2 = it.data().find(',', comma1 + 1);
00165               if (comma2 < 0)
00166               {
00167                 // It's in the old format:  CalendarN=type,calendar
00168                 comma2 = comma1;
00169               }
00170               else
00171               {
00172                 // It's in the format:  calendarN=type,[datetime],calendar
00173                 int secs = it.data().mid(comma1 + 1, comma2 - comma1 - 1).toInt(&ok);
00174                 if (ok)
00175                   dateTime = baseDateTime().addSecs(secs);
00176               }
00177               QString calname = it.data().mid(comma2 + 1);
00178               if ( !calname.isEmpty() ) {
00179                 ADCalendarBase* cal = getCalendar(calname);
00180                 if (cal)
00181                 {
00182                   // The calendar is already in the client's list, so remove
00183                   // this redundant client data file entry.
00184                   if (mIsAlarmDaemon)
00185                     deleteConfigCalendar(cal);
00186                 }
00187                 else
00188                 {
00189                   // Add the calendar to the client's list
00190                   cal = calFactory->create(calname, client,
00191                                static_cast<ADCalendarBase::Type>(it.data().left(comma1).toInt()));
00192                   cal->setRcIndex(rcIndex);
00193                   cal->setLastCheck(dateTime);
00194                   mCalendars.append(cal);
00195                   kdDebug(5900) << "ADConfigDataBase::readConfigData(): calendar " << cal->urlString() << endl;
00196                 }
00197                 newCalendars.append(calname.latin1());
00198               }
00199             }
00200           }
00201         }
00202       }
00203 
00204       if (!newClients.isEmpty())
00205         newClients += ',';
00206       newClients += client;
00207 
00208       // Remove any previous calendars which were not in the client data file
00209       for (ADCalendarBase *cal = mCalendars.first();  cal;  )
00210       {
00211         kdDebug(5900) << "tick..." << endl;
00212         if (cal->appName() == client)
00213         {
00214           if (newCalendars.find(cal->urlString().latin1()) == -1) {
00215             deletedCalendars = true;
00216             mCalendars.remove();
00217             cal = mCalendars.current();
00218             continue;
00219           }
00220         }
00221         cal = mCalendars.next();
00222       }
00223     }
00224   }
00225 
00226   kdDebug(5900) << "ADConfigDataBase::readConfigData() done" << endl;
00227 
00228   return writeNewClients ? newClients : QString::null;
00229 }
00230 
00231 void ADConfigDataBase::deleteConfigCalendar(const ADCalendarBase*)
00232 {
00233 }
00234 
00235 /* Return the ClientInfo structure for the specified client application */
00236 ClientInfo ADConfigDataBase::getClientInfo(const QCString& appName)
00237 {
00238   ClientList::Iterator it;
00239   for( it = mClients.begin(); it != mClients.end(); ++it ) {
00240     if ( (*it).appName == appName ) return *it;
00241   }
00242   return ClientInfo();
00243 }
00244 
00245 void ADConfigDataBase::removeClientInfo( const QCString &appName )
00246 {
00247   ClientList::Iterator it;
00248   for( it = mClients.begin(); it != mClients.end(); ++it ) {
00249     if ( (*it).appName == appName ) {
00250       mClients.remove(it);
00251       break;
00252     }
00253   }
00254 }
00255 
00256 /* Return the ADCalendarBase structure for the specified full calendar URL */
00257 ADCalendarBase *ADConfigDataBase::getCalendar(const QString& calendarURL)
00258 {
00259   if (!calendarURL.isEmpty())
00260   {
00261     for (ADCalendarBase *cal = mCalendars.first();  cal;  cal = mCalendars.next())
00262     {
00263       if (cal->urlString() == calendarURL)
00264         return cal;
00265     }
00266   }
00267   return 0L;
00268 }
00269 
00270 /*
00271  * Expand a DCOP call parameter URL to a full URL.
00272  * (We must store full URLs in the calendar data since otherwise
00273  *  later calls to reload or remove calendars won't necessarily
00274  *  find a match.)
00275  */
00276 QString ADConfigDataBase::expandURL(const QString& urlString)
00277 {
00278   if (urlString.isEmpty())
00279     return QString();
00280   return KURL(urlString).url();
00281 }
00282 
00283 const QDateTime& ADConfigDataBase::baseDateTime()
00284 {
00285   static const QDateTime bdt(QDate(1970,1,1), QTime(0,0,0));
00286   return bdt;
00287 }
KDE Logo
This file is part of the documentation for kalarmd Library Version 3.2.2.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Sat May 1 11:38:04 2004 by doxygen 1.2.15 written by Dimitri van Heesch, © 1997-2003