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
|
/*
KAppfinder, the KDE application finder
Copyright (c) 2002-2003 Tobias Koenig <tokoe@kde.org>
Based on code written by Matthias Hoelzer-Kluepfel <hoelzer@kde.org>
This program is free software; you can redistribute it and/or
modify it under the terms of version 2 of the GNU General Public
License as published by the Free Software Foundation.
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.
*/
// Own
#include "common.h"
// std
#include <stdlib.h>
// Qt
#include <QtCore/QDir>
#include <QtCore/QFile>
// KDE
#include <kdebug.h>
#include <kdesktopfile.h>
#include <kglobal.h>
#include <kstandarddirs.h>
#include <kconfiggroup.h>
#define DBG_CODE 0
void copyFile( const QString &inFileName, const QString &outFileName )
{
QFile inFile( inFileName );
if ( inFile.open( QIODevice::ReadOnly ) ) {
QFile outFile( outFileName );
if ( outFile.open( QIODevice::WriteOnly ) ) {
outFile.write( inFile.readAll() );
outFile.close();
}
inFile.close();
}
}
bool scanDesktopFile( QList<AppLnkCache*> &appCache, const QString &templ,
QString destDir )
{
KDesktopFile desktop( templ );
// find out where to put the .desktop files
QString destName;
if ( destDir.isEmpty() )
destDir = KGlobal::dirs()->saveLocation( "apps" );
else
destDir += '/';
// find out the name of the file to store
destName = templ;
int pos = templ.indexOf( "kappfinder/apps/" );
if ( pos > 0 )
destName = destName.mid( pos + strlen( "kappfinder/apps/" ) );
// calculate real dir and filename
destName = destDir + destName;
pos = destName.lastIndexOf( '/' );
if ( pos > 0 ) {
destDir = destName.left( pos );
destName = destName.mid( pos + 1 );
}
// determine for which executable to look
QString exec = desktop.desktopGroup().readPathEntry( "TryExec", QString() );
if ( exec.isEmpty() )
exec = desktop.desktopGroup().readPathEntry( "Exec", QString() );
pos = exec.indexOf( ' ' );
if ( pos > 0 )
exec = exec.left( pos );
// try to locate the binary
QString pexec = KGlobal::dirs()->findExe( exec,
QString( ::getenv( "PATH" ) ) + ":/usr/X11R6/bin:/usr/games" );
if ( pexec.isEmpty() ) {
kDebug(DBG_CODE) << "looking for " << exec << "\t\tnot found";
return false;
}
AppLnkCache *cache = new AppLnkCache;
cache->destDir = destDir;
cache->destName = destName;
cache->templ = templ;
cache->item = 0;
appCache.append( cache );
kDebug(DBG_CODE) << "looking for " << exec << "\t\tfound";
return true;
}
void createDesktopFiles( QList<AppLnkCache*> &appCache, int &added )
{
AppLnkCache* cache;
Q_FOREACH( cache,appCache ){
if ( cache->item == 0 || ( cache->item && cache->item->checkState(0) == Qt::Checked ) ) {
added++;
QString destDir = cache->destDir;
QString destName = cache->destName;
QString templ = cache->templ;
destDir += '/';
QDir d;
int pos = -1;
while ( ( pos = destDir.indexOf( '/', pos + 1 ) ) >= 0 ) {
QString path = destDir.left( pos + 1 );
d = path;
if ( !d.exists() )
d.mkdir( path );
}
// write out the desktop file
copyFile( templ, destDir + '/' + destName );
}
}
}
void decorateDirs( QString destDir )
{
// find out where to put the .directory files
if ( destDir.isEmpty() )
destDir = KGlobal::dirs()->saveLocation( "apps" );
else
destDir += '/';
const QStringList dirs = KGlobal::dirs()->findAllResources( "data", "kappfinder/apps/*.directory", KStandardDirs::Recursive );
QStringList::const_iterator it;
for ( it = dirs.constBegin(); it != dirs.constEnd(); ++it ) {
// find out the name of the file to store
QString destName = *it;
int pos = destName.indexOf( "kappfinder/apps/" );
if ( pos > 0 )
destName = destName.mid( pos + strlen( "kappfinder/apps/" ) );
destName = destDir + '/' + destName;
if ( !QFile::exists( destName ) ) {
kDebug(DBG_CODE) << "Copy " << *it << " to " << destName;
copyFile( *it, destName );
}
}
}
|