add more spacing
[personal-kdebase.git] / apps / kappfinder / common.cpp
blobb4280c10dfb2b440eec643242c356f6958eea803
1 /*
2 KAppfinder, the KDE application finder
4 Copyright (c) 2002-2003 Tobias Koenig <tokoe@kde.org>
6 Based on code written by Matthias Hoelzer-Kluepfel <hoelzer@kde.org>
8 This program is free software; you can redistribute it and/or
9 modify it under the terms of version 2 of the GNU General Public
10 License as published by the Free Software Foundation.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 // Own
23 #include "common.h"
25 // std
26 #include <stdlib.h>
28 // Qt
29 #include <QtCore/QDir>
30 #include <QtCore/QFile>
32 // KDE
33 #include <kdebug.h>
34 #include <kdesktopfile.h>
35 #include <kglobal.h>
36 #include <kstandarddirs.h>
37 #include <kconfiggroup.h>
39 #define DBG_CODE 1213
41 void copyFile( const QString &inFileName, const QString &outFileName )
43 QFile inFile( inFileName );
44 if ( inFile.open( QIODevice::ReadOnly ) ) {
45 QFile outFile( outFileName );
46 if ( outFile.open( QIODevice::WriteOnly ) ) {
47 outFile.write( inFile.readAll() );
48 outFile.close();
51 inFile.close();
55 bool scanDesktopFile( QList<AppLnkCache*> &appCache, const QString &templ,
56 QString destDir )
58 KDesktopFile desktop( templ );
60 // find out where to put the .desktop files
61 QString destName;
62 if ( destDir.isNull() )
63 destDir = KGlobal::dirs()->saveLocation( "apps" );
64 else
65 destDir += '/';
67 // find out the name of the file to store
68 destName = templ;
69 int pos = templ.indexOf( "kappfinder/apps/" );
70 if ( pos > 0 )
71 destName = destName.mid( pos + strlen( "kappfinder/apps/" ) );
73 // calculate real dir and filename
74 destName = destDir + destName;
75 pos = destName.lastIndexOf( '/' );
76 if ( pos > 0 ) {
77 destDir = destName.left( pos );
78 destName = destName.mid( pos + 1 );
81 // determine for which executable to look
82 QString exec = desktop.desktopGroup().readPathEntry( "TryExec", QString() );
83 if ( exec.isEmpty() )
84 exec = desktop.desktopGroup().readPathEntry( "Exec", QString() );
85 pos = exec.indexOf( ' ' );
86 if ( pos > 0 )
87 exec = exec.left( pos );
89 // try to locate the binary
90 QString pexec = KGlobal::dirs()->findExe( exec,
91 QString( ::getenv( "PATH" ) ) + ":/usr/X11R6/bin:/usr/games" );
92 if ( pexec.isEmpty() ) {
93 kDebug(DBG_CODE) << "looking for " << exec << "\t\tnot found";
94 return false;
97 AppLnkCache *cache = new AppLnkCache;
98 cache->destDir = destDir;
99 cache->destName = destName;
100 cache->templ = templ;
101 cache->item = 0;
103 appCache.append( cache );
105 kDebug(DBG_CODE) << "looking for " << exec << "\t\tfound";
106 return true;
109 void createDesktopFiles( QList<AppLnkCache*> &appCache, int &added )
111 AppLnkCache* cache;
112 Q_FOREACH( cache,appCache ){
113 if ( cache->item == 0 || ( cache->item && cache->item->checkState(0) == Qt::Checked ) ) {
114 added++;
116 QString destDir = cache->destDir;
117 QString destName = cache->destName;
118 QString templ = cache->templ;
120 destDir += '/';
121 QDir d;
122 int pos = -1;
123 while ( ( pos = destDir.indexOf( '/', pos + 1 ) ) >= 0 ) {
124 QString path = destDir.left( pos + 1 );
125 d = path;
126 if ( !d.exists() )
127 d.mkdir( path );
130 // write out the desktop file
131 copyFile( templ, destDir + '/' + destName );
136 void decorateDirs( QString destDir )
138 // find out where to put the .directory files
139 if ( destDir.isNull() )
140 destDir = KGlobal::dirs()->saveLocation( "apps" );
141 else
142 destDir += '/';
144 const QStringList dirs = KGlobal::dirs()->findAllResources( "data", "kappfinder/apps/*.directory", KStandardDirs::Recursive );
146 QStringList::const_iterator it;
147 for ( it = dirs.constBegin(); it != dirs.constEnd(); ++it ) {
148 // find out the name of the file to store
149 QString destName = *it;
150 int pos = destName.indexOf( "kappfinder/apps/" );
151 if ( pos > 0 )
152 destName = destName.mid( pos + strlen( "kappfinder/apps/" ) );
154 destName = destDir + '/' + destName;
156 if ( !QFile::exists( destName ) ) {
157 kDebug(DBG_CODE) << "Copy " << *it << " to " << destName;
158 copyFile( *it, destName );