summaryrefslogtreecommitdiffstats
path: root/kalarm/kalarmd/adconfigdata.cpp
blob: c8c5dd35527754bcc1867df7ffdd984732d5e362 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/*
 *  adcalendar.cpp  -  configuration file access
 *  Program:  KAlarm's alarm daemon (kalarmd)
 *  Copyright (C) 2001, 2004 by David Jarvie <software@astrojar.org.uk>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License along
 *  with this program; if not, write to the Free Software Foundation, Inc.,
 *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 */

#include "kalarmd.h"

#include <tqregexp.h>
#include <tqstringlist.h>

#include <kconfig.h>
#include <kstandarddirs.h>
#include <kdebug.h>

#include "adcalendar.h"
#include "adconfigdata.h"

// Config file key strings
const TQString CLIENT_GROUP(TQString::fromLatin1("Client "));
const TQRegExp CLIENT_GROUP_SEARCH("^Client ");
// Client data file key strings
const TQString CALENDAR_KEY(TQString::fromLatin1("Calendar"));
const TQString TITLE_KEY(TQString::fromLatin1("Title"));
const TQString DCOP_OBJECT_KEY(TQString::fromLatin1("DCOP object"));
const TQString START_CLIENT_KEY(TQString::fromLatin1("Start"));


/******************************************************************************
* Read the configuration file.
* Create the client list and open all calendar files.
*/
void ADConfigData::readConfig()
{
	kdDebug(5900) << "ADConfigData::readConfig()" << endl;
	ClientInfo::clear();
	KConfig* config = KGlobal::config();
	TQStringList clients = config->groupList().grep(CLIENT_GROUP_SEARCH);
	for (TQStringList::Iterator cl = clients.begin();  cl != clients.end();  ++cl)
	{
		// Read this client's configuration
		config->setGroup(*cl);
		TQString client = *cl;
		client.remove(CLIENT_GROUP_SEARCH);
		TQString  title       = config->readEntry(TITLE_KEY, client);   // read app title (default = app name)
		TQCString dcopObject  = config->readEntry(DCOP_OBJECT_KEY).local8Bit();
		bool     startClient = config->readBoolEntry(START_CLIENT_KEY, false);
		TQString  calendar    = config->readPathEntry(CALENDAR_KEY);

		// Verify the configuration
		bool ok = false;
		if (client.isEmpty()  ||  KStandardDirs::findExe(client).isNull())
			kdError(5900) << "ADConfigData::readConfig(): group '" << *cl << "' deleted (client app not found)\n";
		else if (calendar.isEmpty())
			kdError(5900) << "ADConfigData::readConfig(): no calendar specified for '" << client << "'\n";
		else if (dcopObject.isEmpty())
			kdError(5900) << "ADConfigData::readConfig(): no DCOP object specified for '" << client << "'\n";
		else
		{
			ADCalendar* cal = ADCalendar::getCalendar(calendar);
			if (cal)
				kdError(5900) << "ADConfigData::readConfig(): calendar registered by multiple clients: " << calendar << endl;
			else
				ok = true;
		}
		if (!ok)
		{
			config->deleteGroup(*cl, true);
			continue;
		}

		// Create the client and calendar objects
		new ClientInfo(client.local8Bit(), title, dcopObject, calendar, startClient);
		kdDebug(5900) << "ADConfigData::readConfig(): client " << client << " : calendar " << calendar << endl;
	}

	// Remove obsolete CheckInterval entry (if it exists)
        config->setGroup("General");
	config->deleteEntry("CheckInterval");

	// Save any updates
	config->sync();
}

/******************************************************************************
* Write a client application's details to the config file.
*/
void ADConfigData::writeClient(const TQCString& appName, const ClientInfo* cinfo)
{
	KConfig* config = KGlobal::config();
	config->setGroup(CLIENT_GROUP + TQString::fromLocal8Bit(appName));
	config->writeEntry(TITLE_KEY, cinfo->title());
	config->writeEntry(DCOP_OBJECT_KEY, TQString::fromLocal8Bit(cinfo->dcopObject()));
	config->writeEntry(START_CLIENT_KEY, cinfo->startClient());
	config->writePathEntry(CALENDAR_KEY, cinfo->calendar()->urlString());
	config->sync();
}

/******************************************************************************
* Remove a client application's details from the config file.
*/
void ADConfigData::removeClient(const TQCString& appName)
{
	KConfig* config = KGlobal::config();
	config->deleteGroup(CLIENT_GROUP + TQString::fromLocal8Bit(appName));
	config->sync();
}

/******************************************************************************
* Set the calendar file URL for a specified application.
*/
void ADConfigData::setCalendar(const TQCString& appName, ADCalendar* cal)
{
	KConfig* config = KGlobal::config();
	config->setGroup(CLIENT_GROUP + TQString::fromLocal8Bit(appName));
	config->writePathEntry(CALENDAR_KEY, cal->urlString());
        config->sync();
}

/******************************************************************************
* DCOP call to set autostart at login on or off.
*/
void ADConfigData::enableAutoStart(bool on)
{
        kdDebug(5900) << "ADConfigData::enableAutoStart(" << on << ")\n";
        KConfig* config = KGlobal::config();
	config->reparseConfiguration();
        config->setGroup(TQString::fromLatin1(DAEMON_AUTOSTART_SECTION));
        config->writeEntry(TQString::fromLatin1(DAEMON_AUTOSTART_KEY), on);
        config->sync();
}