summaryrefslogtreecommitdiffstats
path: root/tdecore/kdesktopfile.cpp
blob: 29c30f9d98c0e7abcfb2dda9f8b3eddb404ffa04 (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
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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
/*
  This file is part of the KDE libraries
  Copyright (c) 1999 Pietro Iglio <iglio@kde.org>
  Copyright (c) 1999 Preston Brown <pbrown@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 as published by the Free Software Foundation; either
  version 2 of the License, or (at your option) any later version.

  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.
*/

// $Id$

#include <stdlib.h>
#include <unistd.h>

#include <tqfile.h>
#include <tqdir.h>
#include <tqtextstream.h>

#include <kdebug.h>
#include "kurl.h"
#include "kconfigbackend.h"
#include "kapplication.h"
#include "kstandarddirs.h"
#include "kmountpoint.h"
#include "kcatalogue.h"
#include "klocale.h"

#include "kdesktopfile.h"
#include "kdesktopfile.moc"

KDesktopFile::KDesktopFile(const TQString &fileName, bool bReadOnly,
			   const char * resType)
  : KConfig(TQString::fromLatin1(""), bReadOnly, false)
{
  // KConfigBackEnd will try to locate the filename that is provided
  // based on the resource type specified, _only_ if the filename
  // is not an absolute path.
  backEnd->changeFileName(fileName, resType, false);
  setReadOnly(bReadOnly);
  reparseConfiguration();
  setDesktopGroup();
}

KDesktopFile::~KDesktopFile()
{
  // no need to do anything
}

TQString KDesktopFile::locateLocal(const TQString &path)
{
  TQString local;
  if (path.endsWith(".directory"))
  {
    local = path;
    if (!TQDir::isRelativePath(local))
    {
      // Relative wrt apps?
      local = TDEGlobal::dirs()->relativeLocation("apps", path);
    }

    if (TQDir::isRelativePath(local))
    {
      local = ::locateLocal("apps", local); // Relative to apps
    }
    else
    {
      // XDG Desktop menu items come with absolute paths, we need to 
      // extract their relative path and then build a local path.
      local = TDEGlobal::dirs()->relativeLocation("xdgdata-dirs", local);
      if (!TQDir::isRelativePath(local))
      {
        // Hm, that didn't work...
        // What now? Use filename only and hope for the best.
        local = path.mid(path.findRev('/')+1);
      }
      local = ::locateLocal("xdgdata-dirs", local);
    }
  }
  else
  {
    if (TQDir::isRelativePath(path))
    {
      local = ::locateLocal("apps", path); // Relative to apps
    }
    else
    {
      // XDG Desktop menu items come with absolute paths, we need to 
      // extract their relative path and then build a local path.
      local = TDEGlobal::dirs()->relativeLocation("xdgdata-apps", path);
      if (!TQDir::isRelativePath(local))
      {
        // What now? Use filename only and hope for the best.
        local = path.mid(path.findRev('/')+1);
      }
      local = ::locateLocal("xdgdata-apps", local);
    }
  }
  return local;
}

bool KDesktopFile::isDesktopFile(const TQString& path)
{
  int len = path.length();

  if(len > 8 && path.right(8) == TQString::fromLatin1(".desktop"))
    return true;
  else if(len > 7 && path.right(7) == TQString::fromLatin1(".kdelnk"))
    return true;
  else
    return false;
}

bool KDesktopFile::isAuthorizedDesktopFile(const TQString& path)
{
  if (!kapp || kapp->authorize("run_desktop_files"))
     return true;

  if (path.isEmpty())
     return false; // Empty paths are not ok.
  
  if (TQDir::isRelativePath(path))
     return true; // Relative paths are ok.
     
  KStandardDirs *dirs = TDEGlobal::dirs();
  if (TQDir::isRelativePath( dirs->relativeLocation("apps", path) ))
     return true;
  if (TQDir::isRelativePath( dirs->relativeLocation("xdgdata-apps", path) ))
     return true;
  if (TQDir::isRelativePath( dirs->relativeLocation("services", path) ))
     return true;
  if (dirs->relativeLocation("data", path).startsWith("kdesktop/Desktop"))
     return true;
     
  kdWarning() << "Access to '" << path << "' denied because of 'run_desktop_files' restriction." << endl;
  return false;
}

TQString KDesktopFile::translatedEntry(const char* key) const
{
  if (hasTranslatedKey(key))
    return readEntry(key);

  if (hasKey(key)) {
    TQString value = readEntryUntranslated(key);
    TQString fName = fileName();
    fName = fName.mid(fName.findRev('/')+1);
    TQString po_lookup_key = TQString::fromLatin1(key) + "(" + fName + "): " + value;
    TQString po_value = TDEGlobal::locale()->translate(po_lookup_key.utf8().data());

    if (po_value == po_lookup_key)
      return value;

    return po_value;
  }

  return TQString::null;
} 

TQString KDesktopFile::readType() const
{
  return readEntry("Type");
}

TQString KDesktopFile::readIcon() const
{
  return readEntry("Icon");
}

TQString KDesktopFile::readName() const
{
  return translatedEntry("Name");
}

TQString KDesktopFile::readComment() const
{
  return translatedEntry("Comment");
}

TQString KDesktopFile::readGenericName() const
{
  return translatedEntry("GenericName");
}

TQString KDesktopFile::readPath() const
{
  return readPathEntry("Path");
}

TQString KDesktopFile::readDevice() const
{
  return readEntry("Dev");
}

TQString KDesktopFile::readURL() const
{
    if (hasDeviceType()) {
        TQString device = readDevice();
        KMountPoint::List mountPoints = KMountPoint::possibleMountPoints();
	
        for(KMountPoint::List::ConstIterator it = mountPoints.begin();
            it != mountPoints.end(); ++it)
        {
            KMountPoint *mp = *it;
            if (mp->mountedFrom() == device)
            {
                KURL u;
                u.setPath( mp->mountPoint() );
                return u.url();
            }
        }
        return TQString::null;
    } else {
	TQString url = readPathEntry("URL");
        if ( !url.isEmpty() && !TQDir::isRelativePath(url) )
        {
            // Handle absolute paths as such (i.e. we need to escape them)
            KURL u;
            u.setPath( url );
            return u.url();
        }
        return url;
    }
}

TQStringList KDesktopFile::readActions() const
{
    return readListEntry("Actions", ';');
}

void KDesktopFile::setActionGroup(const TQString &group)
{
    setGroup(TQString::fromLatin1("Desktop Action ") + group);
}

bool KDesktopFile::hasActionGroup(const TQString &group) const
{
  return hasGroup(TQString::fromLatin1("Desktop Action ") + group);
}

bool KDesktopFile::hasLinkType() const
{
  return readEntry("Type") == TQString::fromLatin1("Link");
}

bool KDesktopFile::hasApplicationType() const
{
  return readEntry("Type") == TQString::fromLatin1("Application");
}

bool KDesktopFile::hasMimeTypeType() const
{
  return readEntry("Type") == TQString::fromLatin1("MimeType");
}

bool KDesktopFile::hasDeviceType() const
{
  return readEntry("Type") == TQString::fromLatin1("FSDev") ||
         readEntry("Type") == TQString::fromLatin1("FSDevice");
}

bool KDesktopFile::tryExec() const
{
  // Test for TryExec and "X-TDE-AuthorizeAction" 
  TQString te = readPathEntry("TryExec");

  if (!te.isEmpty()) {
    if (!TQDir::isRelativePath(te)) {
      if (::access(TQFile::encodeName(te), X_OK))
	return false;
    } else {
      // !!! Sergey A. Sukiyazov <corwin@micom.don.ru> !!!
      // Environment PATH may contain filenames in 8bit locale cpecified
      // encoding (Like a filenames).
      TQStringList dirs = TQStringList::split(':', TQFile::decodeName(::getenv("PATH")));
      TQStringList::Iterator it(dirs.begin());
      bool match = false;
      for (; it != dirs.end(); ++it) {
	TQString fName = *it + "/" + te;
	if (::access(TQFile::encodeName(fName), X_OK) == 0)
	{
	  match = true;
	  break;
	}
      }
      // didn't match at all
      if (!match)
        return false;
    }
  }
  TQStringList list = readListEntry("X-TDE-AuthorizeAction");
  if (kapp && !list.isEmpty())
  {
     for(TQStringList::ConstIterator it = list.begin();
         it != list.end();
         ++it)
     {
        if (!kapp->authorize((*it).stripWhiteSpace()))
           return false;
     }
  }
  
  // See also KService::username()
  bool su = readBoolEntry("X-TDE-SubstituteUID");
  if (su)
  {
      TQString user = readEntry("X-TDE-Username");
      if (user.isEmpty())
        user = ::getenv("ADMIN_ACCOUNT");
      if (user.isEmpty())
        user = "root";
      if (!kapp->authorize("user/"+user))
        return false;
  }
  
  return true;
}

/**
 * @return the filename as passed to the constructor.
 */
TQString
KDesktopFile::fileName() const { return backEnd->fileName(); }

/**
 * @return the resource type as passed to the constructor.
 */
TQString
KDesktopFile::resource() const { return backEnd->resource(); }

TQStringList
KDesktopFile::sortOrder() const
{
  return readListEntry("SortOrder");
}

void KDesktopFile::virtual_hook( int id, void* data )
{ KConfig::virtual_hook( id, data ); }

TQString KDesktopFile::readDocPath() const
{
  // Depreciated, remove in KDE4 or 5?
  // See: http://www.freedesktop.org/Standards/desktop-entry-spec
  if(hasKey( "DocPath" ))
    return readPathEntry( "DocPath" );

  return readPathEntry( "X-DocPath" );
}

KDesktopFile* KDesktopFile::copyTo(const TQString &file) const
{
  KDesktopFile *config = new KDesktopFile(TQString::null, false);
  KConfig::copyTo(file, config);
  config->setDesktopGroup();
  return config;
}