summaryrefslogtreecommitdiffstats
path: root/noatun-plugins/oblique/kdatacollection.cpp
blob: ec3485e6f2e36ec5eec714ec34521ffba125b085 (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
/*
	This file is part of the KDE libraries
	Copyright (C) 2003 Charles Samuels <charles@kde.org>

	This library is free software; you can redistribute it and/or
	modify it under the terms of the GNU Library General Public
	License version 2 as published by the Free Software Foundation.

	This library 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
	Library General Public License for more details.

	You should have received a copy of the GNU Library General Public License
	along with this library; see the file COPYING.LIB.  If not, write to
	the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
	Boston, MA 02110-1301, USA.
*/

#include "kdatacollection.h"

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

#include <tqfile.h>
#include <tqfileinfo.h>


KDataCollection::KDataCollection(
		KConfig *config, const TQString &group, const TQString &entry,
		const char *datadir, const TQString &dir
	)
{
	init(config, group, entry, datadir, dir);
}

KDataCollection::KDataCollection(
		KConfig *config, const TQString &group, const TQString &entry,
		const TQString &dir
	)
{
	init(config, group, entry, "appdata", dir);
}

KDataCollection::KDataCollection(
		KConfig *config, const TQString &group, const TQString &dir
	)
{
	init(config, group, dir, "appdata", dir);
}

KDataCollection::KDataCollection(KConfig *config, const TQString &dir)
{
	init(config, "KDataCollection", dir, "appdata", dir);
}

KDataCollection::KDataCollection(const TQString &dir)
{
	init(TDEGlobal::config(), "KDataCollection", dir, "appdata", dir);
}

void KDataCollection::init(
		KConfig *config, const TQString &group, const TQString &entry,
		const char *datadir, const TQString &dir
	)
{
	mConfig = config;
	mGroup = group;
	mEntry = entry;
	mDir = dir;
	mDatadir = datadir;
}

TQStringList KDataCollection::names() const
{
	KConfigGroup g(mConfig, mGroup);

	// these are the entries I have
	TQStringList n = g.readListEntry(mEntry);
	TQStringList fs = TDEGlobal::dirs()->findAllResources(mDatadir, mDir+"/*", false, true);
	TQStringList total;

	for (TQStringList::Iterator i(fs.begin()); i != fs.end(); ++i)
	{
		TQFileInfo fi(*i);
		TQString name = fi.fileName();
		if (!n.contains(name))
		{
			total.append(name);
		}
	}

	return total;
}

void KDataCollection::remove(const TQString &name)
{
	KConfigGroup g(mConfig, mGroup);
	TQString location = file(name);
	if (location.isEmpty()) return;
	if (location == saveFile(name, false))
	{
		TQFile(location).remove();
		// is there a system one too?
		location = file(name, false);
		if (location.isEmpty()) return;
	}

	TQStringList n = g.readListEntry(mEntry);
	if (n.contains(name)) return;
	n.append(name);
	g.writeEntry(mEntry, n);
}

TQString KDataCollection::file(const TQString &name, bool create)
{
	TQString path = ::locate(mDatadir, mDir+"/"+name);

	if (path.isEmpty() && create)
	{
		path = saveFile(name, true);
	}
	return path;
}

TQString KDataCollection::saveFile(const TQString &name, bool create)
{
	if (!TDEGlobal::dirs()->isRestrictedResource(mDatadir, mDir+"/"+name))
	{
		TQString s = TDEGlobal::dirs()->saveLocation(mDatadir, mDir, create);

		if (s.length() && create)
		{
			s += "/" + name;
			TQFile(s).open(IO_ReadWrite); // create it
		}
		return s;
	}
	return TQString();
}