summaryrefslogtreecommitdiffstats
path: root/kappfinder/common.cpp
blob: c6cd95a659e589ec066262200e2a7a16c1cb94f7 (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
/*
    KAppfinder, the KDE application finder

    Copyright (c) 2002-2003 Tobias Koenig <[email protected]>

    Based on code written by Matthias Hoelzer-Kluepfel <[email protected]>

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

#include <kdebug.h>
#include <kdesktopfile.h>
#include <kglobal.h>
#include <kstandarddirs.h>

#include <qdir.h>
#include <qfile.h>

#include <stdlib.h>

#include "common.h"

#define DBG_CODE 1213

void copyFile( const QString &inFileName, const QString &outFileName )
{
  QFile inFile( inFileName );
  if ( inFile.open( IO_ReadOnly ) ) {
    QFile outFile( outFileName );
    if ( outFile.open( IO_WriteOnly ) ) {
      outFile.writeBlock( inFile.readAll() );
      outFile.close();
    }

    inFile.close();
  }
}

bool scanDesktopFile( QPtrList<AppLnkCache> &appCache, const QString &templ,
                      QString destDir )
{
  KDesktopFile desktop( templ, true );

  // find out where to put the .desktop files
  QString destName;
  if ( destDir.isNull() )
    destDir = KGlobal::dirs()->saveLocation( "apps" );
  else
    destDir += "/";

  // find out the name of the file to store
  destName = templ;
  int pos = templ.find( "kappfinder/apps/" );
  if ( pos > 0 )
    destName = destName.mid( pos + strlen( "kappfinder/apps/" ) );

  // calculate real dir and filename
  destName = destDir + destName;
  pos = destName.findRev( '/' );
  if ( pos > 0 ) {
    destDir = destName.left( pos );
    destName = destName.mid( pos + 1 );
  }

  // determine for which executable to look
  QString exec = desktop.readPathEntry( "TryExec" );
  if ( exec.isEmpty() )
    exec = desktop.readPathEntry( "Exec" );
  pos = exec.find( ' ' );
  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() ) {
    kdDebug(DBG_CODE) << "looking for " << exec.local8Bit()
                      << "\t\tnot found" << endl;
    return false;
  }

  AppLnkCache *cache = new AppLnkCache;
  cache->destDir = destDir;
  cache->destName = destName;
  cache->templ = templ;
  cache->item = 0;

  appCache.append( cache );

  kdDebug(DBG_CODE) << "looking for " << exec.local8Bit()
                    << "\t\tfound" << endl;
  return true;
}

void createDesktopFiles( QPtrList<AppLnkCache> &appCache, int &added )
{
  AppLnkCache* cache;
  for ( cache = appCache.first(); cache; cache = appCache.next() ) {
    if ( cache->item == 0 || ( cache->item && cache->item->isOn() ) ) {
      added++;

      QString destDir = cache->destDir;
      QString destName = cache->destName;
      QString templ = cache->templ;

      destDir += "/";
      QDir d;
      int pos = -1;
      while ( ( pos = destDir.find( '/', 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.isNull() )
    destDir = KGlobal::dirs()->saveLocation( "apps" );
  else
    destDir += "/";

  QStringList dirs = KGlobal::dirs()->findAllResources( "data", "kappfinder/apps/*.directory", true );

  QStringList::Iterator it;
  for ( it = dirs.begin(); it != dirs.end(); ++it ) {
    // find out the name of the file to store
    QString destName = *it;
    int pos = destName.find( "kappfinder/apps/" );
    if ( pos > 0 )
      destName = destName.mid( pos + strlen( "kappfinder/apps/" ) );

    destName = destDir + "/" + destName;

    if ( !QFile::exists( destName ) ) {	
      kdDebug(DBG_CODE) << "Copy " << *it << " to " << destName << endl;
      copyFile( *it, destName );
    }
  }
}