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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
|
/***************************************************************************
ksv::IO.cpp - description
-------------------
begin : Sun Oct 3 1999
copyright : (C) 1997-2000 by Peter Putzer
email : putzer@kde.org
***************************************************************************/
/***************************************************************************
* *
* 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; version 2. *
* *
***************************************************************************/
#include <errno.h>
#include <unistd.h>
#include <tqfileinfo.h>
#include <tqptrlist.h>
#include <tqvaluelist.h>
#include <tqstringlist.h>
#include <kdebug.h>
#include <kapplication.h>
#include <klocale.h>
#include <kmimemagic.h>
#include <kservice.h>
#include <kuserprofile.h>
#include "Data.h"
#include "IOCore.h"
#include "ksvdraglist.h"
#include "ksv_core.h"
#include "ksv_conf.h"
TQString ksv::IO::relToAbs (const TQString& dir, const TQString& rel)
{
if (rel.left(1) != "/")
return TQDir::cleanDirPath (dir + "/" + rel);
else
return TQDir::cleanDirPath (rel);
}
void ksv::IO::removeFile (const TQFileInfo& info, TQDir& dir, TQString& rich, TQString& plain)
{
if (!dir.remove(info.fileName(), FALSE))
{
rich = (i18n ("<error>FAILED</error> to remove <cmd>%1</cmd> from <cmd>%2</cmd>: \"%3\"<br/>")
.arg(info.fileName())
.arg(dir.path())
.arg(strerror(errno)));
plain = (i18n ("FAILED to remove %1 from %2: \"%3\"\n")
.arg(info.fileName())
.arg(dir.path())
.arg(strerror(errno)));
}
else
{
rich = i18n("removed <cmd>%1</cmd> from <cmd>%2</cmd><br/>")
.arg(info.fileName())
.arg(dir.path());
plain = i18n("removed %1 from %2\n")
.arg(info.fileName())
.arg(dir.path());
}
}
void ksv::IO::dissectFilename (const TQString& file, TQString& base, int& nr)
{
TQString tmp = file.mid(1, file.length());
nr = tmp.left(2).toInt();
base = tmp.mid(2, tmp.length());
}
void ksv::IO::makeSymlink (const KSVData& data, int runlevel, bool start,
TQString& rich, TQString& plain)
{
const TQString symName = TQString("%1%2%3").arg(start ? "S" : "K").arg(data.numberString()).arg(data.label());
const TQString symPath = TQString("%1/rc%2.d/").arg(KSVConfig::self()->runlevelPath()).arg(runlevel);
const TQString symbol = symPath + symName;
TQString target = data.filename();
if (TQDir::isRelativePath(target))
target = ksv::IO::makeRelativePath(TQDir::cleanDirPath(symPath),
TQDir::cleanDirPath(data.path())) + data.filename();
if (symlink(target.local8Bit(), symbol.local8Bit()) == 0)
{
rich = i18n("created <cmd>%1</cmd> in <cmd>%2</cmd><br/>").arg(symName).arg(symPath);
plain = i18n("created %1 in %2\n").arg(symName).arg(symPath);
}
else
{
rich = i18n("<error>FAILED</error> to create <cmd>%1</cmd> in <cmd>%2</cmd>: \"%3\"<br/>")
.arg(symName)
.arg(symPath)
.arg(strerror(errno));
plain = i18n("FAILED to create %1 in %2: \"%3\"\n")
.arg(symName)
.arg(symPath)
.arg(strerror(errno));
}
}
TQString ksv::IO::makeRelativePath (const TQString& from, const TQString& to)
{
if (TQDir::isRelativePath(from) || TQDir::isRelativePath(to))
return TQString::null;
int pos = 0;
const int f_length = from.length();
TQStringList from_list;
while (pos > -1)
{
const int old = pos + 1;
const int res = from.find('/', old);
int length = 0;
if (res > -1)
length = res - old + 1;
else
length = f_length - old;
from_list.append (from.mid(old, length));
pos = res;
}
const int t_length = to.length();
TQStringList to_list;
pos = 0;
while (pos > -1)
{
const int old = pos + 1;
const int res = to.find('/', old);
int length = 0;
if (res > -1)
length = res - old + 1;
else
length = t_length - old;
to_list.append (to.mid(old, length));
pos = res;
}
int lcp = 0; // longest common prefix
const int f_c = from_list.count();
const int t_c = to_list.count();
while (lcp < f_c && lcp < t_c
&& *from_list.at(lcp) == *to_list.at(lcp))
lcp++;
TQString result;
for (int i = f_c - lcp; i > 0; --i)
result += "../";
for (int i = lcp; i < t_c; ++i)
result += *to_list.at(i) + "/";
return result;
}
bool ksv::IO::loadSavedConfiguration (TQDataStream& s,
TQValueList<KSVData>* start,
TQValueList<KSVData>* stop)
{
TQCString magic;
s >> magic;
if (magic != "KSysV")
return false;
Q_INT32 version = 0;
s >> version;
if (version != 3)
return false; // too old
TQDateTime saveTime;
s >> saveTime;
for (int i = 0; i < ksv::runlevelNumber; ++i)
{
TQString rlMagic;
TQString section;
s >> rlMagic;
s >> section;
if (rlMagic != TQString::fromLatin1("RUNLEVEL %1").arg(i))
return false;
if (section != "START")
return false;
Q_INT32 numberOfItems;
s >> numberOfItems;
KSVData data;
for (int j = 0; j < numberOfItems; ++j)
{
s >> data;
start[i].append (data);
}
s >> section;
if (section != "STOP")
return false;
s >> numberOfItems;
for (int j = 0; j < numberOfItems; ++j)
{
s >> data;
stop[i].append(data);
}
}
return true;
}
bool ksv::IO::saveConfiguration (TQDataStream& s,
KSVDragList** start,
KSVDragList** stop)
{
Q_INT32 version = 3;
s << TQCString("KSysV")
<< version
<< TQDateTime::currentDateTime(); // save date
for (int i = 0; i < ksv::runlevelNumber; ++i)
{
Q_INT32 numberOfItems = start[i]->childCount();
s << TQString::fromLatin1 ("RUNLEVEL %1").arg (i)
<< TQString::fromLatin1 ("START")
<< numberOfItems;
for (TQListViewItemIterator it (start[i]);
it.current();
++it)
{
s << *static_cast<KSVItem*> (it.current())->data();
}
numberOfItems = stop[i]->childCount();
s << TQString::fromLatin1 ("STOP")
<< numberOfItems;
for (TQListViewItemIterator it (stop[i]);
it.current();
++it)
{
s << *static_cast<KSVItem*> (it.current())->data();
}
}
return true;
}
KTrader::OfferList ksv::IO::servicesForFile (const TQString& filename)
{
static KTrader* trader = KTrader::self();
static KMimeMagic* magic = KMimeMagic::self();
const TQString mimetype = magic->findFileType(filename)->mimeType();
return trader->query (mimetype, "Type == 'Application'");
}
KService::Ptr ksv::IO::preferredServiceForFile (const TQString& filename)
{
static KMimeMagic* magic = KMimeMagic::self();
const TQString mimetype = magic->findFileType(filename)->mimeType();
return KServiceTypeProfile::preferredService (mimetype, "Application");
}
|