summaryrefslogtreecommitdiffstats
path: root/kappfinder/toplevel.cpp
blob: e5cf13ec869ed6811b64bbd54c28a3474fe86f8f (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
/*
    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 <kaccelmanager.h>
#include <kapplication.h>
#include <kbuttonbox.h>
#include <kdebug.h>
#include <kdesktopfile.h>
#include <kglobal.h>
#include <kiconloader.h>
#include <klocale.h>
#include <kmessagebox.h>
#include <kprogress.h>
#include <kservice.h>
#include <kservicegroup.h>
#include <kstandarddirs.h>
#include <kstdguiitem.h>
#include <kstartupinfo.h>

#include <tqaccel.h>
#include <tqlabel.h>
#include <tqlayout.h>
#include <tqpushbutton.h>
#include <tqdir.h>
#include <tqregexp.h>

#include "toplevel.h"

TopLevel::TopLevel( const TQString &destDir, TQWidget *parent, const char *name )
  : KDialog( parent, name, true )
{
  setCaption( i18n( "KAppfinder" ) );
  TQVBoxLayout *layout = new TQVBoxLayout( this, marginHint() );

  TQLabel *label = new TQLabel( i18n( "The application finder looks for non-KDE "
                                    "applications on your system and adds "
                                    "them to the KDE menu system. "
                                    "Click 'Scan' to begin, select the desired applications and then click 'Apply'."), this);
  label->tqsetAlignment( AlignAuto | WordBreak );
  layout->addWidget( label );

  layout->addSpacing( 5 );

  mListView = new TQListView( this );
  mListView->addColumn( i18n( "Application" ) );
  mListView->addColumn( i18n( "Description" ) );
  mListView->addColumn( i18n( "Command" ) );
  mListView->setMinimumSize( 300, 300 );
  mListView->setRootIsDecorated( true );
  mListView->setAllColumnsShowFocus( true );
  mListView->setSelectionMode(TQListView::NoSelection);
  layout->addWidget( mListView );

  mProgress = new KProgress( this );
  mProgress->setPercentageVisible( false );
  layout->addWidget( mProgress );

  mSummary = new TQLabel( i18n( "Summary:" ), this );
  layout->addWidget( mSummary );

  KButtonBox* bbox = new KButtonBox( this );
  mScanButton = bbox->addButton( KGuiItem( i18n( "Scan" ), "find"), TQT_TQOBJECT(this), TQT_SLOT( slotScan() ) );
  bbox->addStretch( 5 );
  mSelectButton = bbox->addButton( i18n( "Select All" ), TQT_TQOBJECT(this),
                                   TQT_SLOT( slotSelectAll() ) );
  mSelectButton->setEnabled( false );
  mUnSelectButton = bbox->addButton( i18n( "Unselect All" ), TQT_TQOBJECT(this),
                                     TQT_SLOT( slotUnselectAll() ) );
  mUnSelectButton->setEnabled( false );
  bbox->addStretch( 5 );
  mApplyButton = bbox->addButton( KStdGuiItem::apply(), TQT_TQOBJECT(this), TQT_SLOT( slotCreate() ) );
  mApplyButton->setEnabled( false );
  bbox->addButton( KStdGuiItem::close(), TQT_TQOBJECT(kapp), TQT_SLOT( quit() ) );
  bbox->layout();

  layout->addWidget( bbox );

	connect( kapp, TQT_SIGNAL( lastWindowClosed() ), kapp, TQT_SLOT( quit() ) );

  mAppCache.setAutoDelete( true );

  adjustSize();

  mDestDir = destDir;
  mDestDir = mDestDir.replace( TQRegExp( "^~/" ), TQDir::homeDirPath() + "/" );
	
  KStartupInfo::appStarted();

  TQAccel *accel = new TQAccel( this );
  accel->connectItem( accel->insertItem( Key_Q + CTRL ), kapp, TQT_SLOT( quit() ) );

  KAcceleratorManager::manage( this );
}


TopLevel::~TopLevel()
{
  mAppCache.clear();
}

TQListViewItem* TopLevel::addGroupItem( TQListViewItem *parent, const TQString &relPath,
                                       const TQString &name )
{
  KServiceGroup::Ptr root = KServiceGroup::group( relPath );
  if( !root )
		  return 0L;
  KServiceGroup::List list = root->entries();

  KServiceGroup::List::ConstIterator it;
  for ( it = list.begin(); it != list.end(); ++it ) {
    KSycocaEntry *p = (*it);
    if ( p->isType( KST_KServiceGroup ) ) {
      KServiceGroup* serviceGroup = static_cast<KServiceGroup*>( p );
      if ( TQString( "%1%2/" ).arg( relPath ).arg( name ) == serviceGroup->relPath() ) {
        TQListViewItem* retval;
        if ( parent )
          retval = parent->firstChild();
        else
          retval = mListView->firstChild();

        while ( retval ) {
          if ( retval->text( 0 ) == serviceGroup->caption() )
            return retval;

          retval = retval->nextSibling();
        }

        TQListViewItem *item;
        if ( parent )
          item = new TQListViewItem( parent, serviceGroup->caption() );
        else
          item = new TQListViewItem( mListView, serviceGroup->caption() );

        item->setPixmap( 0, SmallIcon( serviceGroup->icon() ) );
        item->setOpen( true );
        return item;
      }
    }
  }

  return 0;
}

void TopLevel::slotScan()
{
  KIconLoader* loader = KGlobal::iconLoader();

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

  mAppCache.clear();

  mFound = 0;
  int count = mTemplates.count();

  mScanButton->setEnabled( false );
  mProgress->setPercentageVisible( true );
  mProgress->setTotalSteps( count );
  mProgress->setValue( 0 );

  mListView->clear();

  TQStringList::Iterator it;
  for ( it = mTemplates.begin(); it != mTemplates.end(); ++it ) {
    // eye candy
    mProgress->setProgress( mProgress->progress() + 1 );

    TQString desktopName = *it;
    int i = desktopName.findRev('/');
    desktopName = desktopName.mid(i+1);
    i = desktopName.findRev('.');
    if (i != -1)
       desktopName = desktopName.left(i);

    bool found;
    found = KService::serviceByDesktopName(desktopName);
    if (found)
       continue;

    found = KService::serviceByMenuId("kde-"+desktopName+".desktop");
    if (found)
       continue; 

    found = KService::serviceByMenuId("gnome-"+desktopName+".desktop");
    if (found)
       continue; 

    KDesktopFile desktop( *it, true );

    // copy over the desktop file, if exists
    if ( scanDesktopFile( mAppCache, *it, mDestDir ) ) {
      TQString relPath = *it;
      int pos = relPath.find( "kappfinder/apps/" );
      relPath = relPath.mid( pos + strlen( "kappfinder/apps/" ) );
      relPath = relPath.left( relPath.findRev( '/' ) + 1 );
      TQStringList dirList = TQStringList::split( '/', relPath );

      TQListViewItem *dirItem = 0;
      TQString tmpRelPath = TQString::null;

      TQStringList::Iterator tmpIt;
      for ( tmpIt = dirList.begin(); tmpIt != dirList.end(); ++tmpIt ) {
        dirItem = addGroupItem( dirItem, tmpRelPath, *tmpIt );
        tmpRelPath += *tmpIt + '/';
      }

      mFound++;

      TQCheckListItem *item;
      if ( dirItem )
        item = new TQCheckListItem( dirItem, desktop.readName(), TQCheckListItem::CheckBox );
      else
        item = new TQCheckListItem( mListView, desktop.readName(), TQCheckListItem::CheckBox );

      item->setPixmap( 0, loader->loadIcon( desktop.readIcon(), KIcon::Small ) );
      item->setText( 1, desktop.readGenericName() );
      item->setText( 2, desktop.readPathEntry( "Exec" ) );
      if ( desktop.readBoolEntry( "X-StandardInstall" ) )
        item->setOn( true );

      AppLnkCache* cache = mAppCache.last();
      if ( cache )
        cache->item = item;
    }

    // update summary
    TQString sum( i18n( "Summary: found %n application",
                       "Summary: found %n applications", mFound ) );
    mSummary->setText( sum );
  }

  // stop scanning
  mProgress->setValue( 0 );
  mProgress->setPercentageVisible( false );

  mScanButton->setEnabled( true );

  if ( mFound > 0 ) {
    mApplyButton->setEnabled( true );
    mSelectButton->setEnabled( true );
    mUnSelectButton->setEnabled( true );
  }
}

void TopLevel::slotSelectAll()
{
  AppLnkCache* cache;
  for ( cache = mAppCache.first(); cache; cache = mAppCache.next() )
    cache->item->setOn( true );
}

void TopLevel::slotUnselectAll()
{
  AppLnkCache* cache;
  for ( cache = mAppCache.first(); cache; cache = mAppCache.next() )
    cache->item->setOn( false );
}

void TopLevel::slotCreate()
{
  // copy template files
  mAdded = 0;
  createDesktopFiles( mAppCache, mAdded );

  // decorate directories
  decorateDirs( mDestDir );

  KService::rebuildKSycoca(this);

  TQString message( i18n( "%n application was added to the KDE menu system.",
                         "%n applications were added to the KDE menu system.", mAdded ) );
  KMessageBox::information( this, message, TQString::null, "ShowInformation" );
}

#include "toplevel.moc"