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.
29 #include <QtCore/QDir>
30 #include <QtCore/QFile>
34 #include <kdesktopfile.h>
36 #include <kstandarddirs.h>
37 #include <kconfiggroup.h>
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() );
55 bool scanDesktopFile( QList
<AppLnkCache
*> &appCache
, const QString
&templ
,
58 KDesktopFile
desktop( templ
);
60 // find out where to put the .desktop files
62 if ( destDir
.isNull() )
63 destDir
= KGlobal::dirs()->saveLocation( "apps" );
67 // find out the name of the file to store
69 int pos
= templ
.indexOf( "kappfinder/apps/" );
71 destName
= destName
.mid( pos
+ strlen( "kappfinder/apps/" ) );
73 // calculate real dir and filename
74 destName
= destDir
+ destName
;
75 pos
= destName
.lastIndexOf( '/' );
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() );
84 exec
= desktop
.desktopGroup().readPathEntry( "Exec", QString() );
85 pos
= exec
.indexOf( ' ' );
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";
97 AppLnkCache
*cache
= new AppLnkCache
;
98 cache
->destDir
= destDir
;
99 cache
->destName
= destName
;
100 cache
->templ
= templ
;
103 appCache
.append( cache
);
105 kDebug(DBG_CODE
) << "looking for " << exec
<< "\t\tfound";
109 void createDesktopFiles( QList
<AppLnkCache
*> &appCache
, int &added
)
112 Q_FOREACH( cache
,appCache
){
113 if ( cache
->item
== 0 || ( cache
->item
&& cache
->item
->checkState(0) == Qt::Checked
) ) {
116 QString destDir
= cache
->destDir
;
117 QString destName
= cache
->destName
;
118 QString templ
= cache
->templ
;
123 while ( ( pos
= destDir
.indexOf( '/', pos
+ 1 ) ) >= 0 ) {
124 QString path
= destDir
.left( pos
+ 1 );
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" );
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/" );
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
);