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
|
/*
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 "toplevel.h"
// Qt
#include <QtGui/QLabel>
#include <QtGui/QLayout>
#include <QtGui/QPushButton>
#include <QtCore/QDir>
#include <QtCore/QRegExp>
#include <QtGui/QProgressBar>
#include <QtGui/QBoxLayout>
// KDE
#include <kapplication.h>
#include <kbuildsycocaprogressdialog.h>
#include <kdebug.h>
#include <kdesktopfile.h>
#include <kglobal.h>
#include <klocale.h>
#include <kmessagebox.h>
#include <kservice.h>
#include <kservicegroup.h>
#include <kstandarddirs.h>
#include <kstandardguiitem.h>
TopLevel::TopLevel( const QString &destDir, QWidget *parent )
: KDialog( parent )
{
setCaption( i18n( "KAppfinder" ) );
setButtons( Apply | Close | User1 | User2 | User3 );
QFrame *frame = new QFrame;
setMainWidget( frame );
QVBoxLayout *layout = new QVBoxLayout( frame );
QLabel *label = new QLabel( 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'."), frame);
label->setAlignment( Qt::AlignLeft );
label->setWordWrap( true );
mListView = new QTreeWidget( frame );
mListView->setHeaderLabels(QStringList() << i18n( "Application" ) << i18n( "Description" ) << i18n( "Command" ) );
mListView->setRootIsDecorated( true );
mListView->setAllColumnsShowFocus( true );
mListView->setSelectionMode( QTreeWidget::NoSelection );
mListView->setColumnWidth( 0, 200 );
mListView->setColumnWidth( 1, 160 );
mProgress = new QProgressBar( frame );
mProgress->setTextVisible( false );
mSummary = new QLabel( i18n( "Summary:" ), frame );
setButtonGuiItem(User1 , KGuiItem( i18n( "Scan" ), "edit-find"));
setButtonGuiItem(User2, KGuiItem(i18n( "Select All" )));
setButtonGuiItem(User3, KGuiItem(i18n( "Unselect All" )));
connect(this,SIGNAL(user1Clicked()), this, SLOT( slotScan() ) );
connect(this,SIGNAL(user2Clicked()), this, SLOT( slotSelectAll() ) );
connect(this,SIGNAL(user3Clicked()), this, SLOT( slotUnselectAll() ) );
connect(this,SIGNAL(applyClicked()), this, SLOT( slotCreate() ) );
connect(this,SIGNAL(closeClicked()), kapp, SLOT( quit() ) );
enableButton(User2, false );
enableButton(User3, false );
enableButton(Apply, false );
layout->addWidget( label );
layout->addSpacing( spacingHint() );
layout->addWidget( mListView );
layout->addWidget( mProgress );
layout->addWidget( mSummary );
connect( kapp, SIGNAL( lastWindowClosed() ), kapp, SLOT( quit() ) );
adjustSize();
resize( 2 * width(), height() );
setMinimumSize(minimumSizeHint());
mDestDir = destDir;
mDestDir = mDestDir.replace( QRegExp( "^~/" ), QDir::homePath() + '/' );
}
TopLevel::~TopLevel()
{
qDeleteAll(mAppCache);
mAppCache.clear();
}
QTreeWidgetItem* TopLevel::addGroupItem( QTreeWidgetItem *parent, const QString &relPath,
const QString &name )
{
KServiceGroup::Ptr root = KServiceGroup::group( relPath );
if( !root )
return 0L;
KServiceGroup::List list = root->entries();
KServiceGroup::List::ConstIterator it;
for ( it = list.constBegin(); it != list.constEnd(); ++it ) {
const KSycocaEntry *p = (*it).data();
if ( p->isType( KST_KServiceGroup ) ) {
const KServiceGroup* serviceGroup = static_cast<const KServiceGroup*>( p );
if ( QString( "%1%2/" ).arg( relPath ).arg( name ) == serviceGroup->relPath() ) {
QTreeWidgetItemIterator it( mListView );
if ( parent )
it = QTreeWidgetItemIterator( parent );
while ( *it ) {
if ( ( *it )->text( 0 ) == serviceGroup->caption() )
return ( *it );
it++;
}
QTreeWidgetItem *item;
if ( parent )
item = new QTreeWidgetItem( parent, QStringList() << serviceGroup->caption() );
else
item = new QTreeWidgetItem( mListView, QStringList() << serviceGroup->caption() );
item->setIcon( 0, KIcon( serviceGroup->icon() ) );
item->setExpanded( true );
return item;
}
}
}
return 0;
}
void TopLevel::slotScan()
{
mTemplates = KGlobal::dirs()->findAllResources( "data", "kappfinder/apps/*.desktop", KStandardDirs::Recursive );
mAppCache.clear();
mFound = 0;
int count = mTemplates.count();
enableButton(User1, false ); //disable scan button
mProgress->setTextVisible( true );
mProgress->setRange( 0, count );
mProgress->setValue( 0 );
mListView->clear();
QStringList::const_iterator it;
for ( it = mTemplates.constBegin(); it != mTemplates.constEnd(); ++it ) {
// eye candy
mProgress->setValue( mProgress->value() + 1 );
QString desktopName = *it;
int i = desktopName.lastIndexOf('/');
desktopName = desktopName.mid(i+1);
i = desktopName.lastIndexOf('.');
if (i != -1)
desktopName = desktopName.left(i);
bool found;
found = KService::serviceByDesktopName(desktopName);
if (found)
continue;
found = KService::serviceByMenuId("kde4-"+desktopName+".desktop");
if (found)
continue;
found = KService::serviceByMenuId("gnome-"+desktopName+".desktop");
if (found)
continue;
KDesktopFile desktop( *it );
// copy over the desktop file, if exists
if ( scanDesktopFile( mAppCache, *it, mDestDir ) ) {
QString relPath = *it;
int pos = relPath.indexOf( "kappfinder/apps/" );
relPath = relPath.mid( pos + strlen( "kappfinder/apps/" ) );
relPath = relPath.left( relPath.lastIndexOf( '/' ) + 1 );
QStringList dirList = relPath.split( '/');
QTreeWidgetItem *dirItem = 0;
QString tmpRelPath;
QStringList::const_iterator tmpIt;
for ( tmpIt = dirList.constBegin(); tmpIt != dirList.constEnd(); ++tmpIt ) {
dirItem = addGroupItem( dirItem, tmpRelPath, *tmpIt );
tmpRelPath += *tmpIt + '/';
}
mFound++;
QTreeWidgetItem *item;
if ( dirItem )
item = new QTreeWidgetItem( dirItem, QStringList() << desktop.readName() );
else
item = new QTreeWidgetItem( mListView, QStringList() << desktop.readName() );
item->setIcon( 0, KIcon( desktop.readIcon() ) );
item->setText( 1, desktop.readGenericName() );
item->setText( 2, desktop.desktopGroup().readPathEntry( "Exec", QString() ) );
if ( desktop.desktopGroup().readEntry( "X-StandardInstall" , false) )
item->setCheckState( 0, Qt::Checked );
else
item->setCheckState( 0, Qt::Unchecked );
AppLnkCache* cache = mAppCache.last();
if ( cache )
cache->item = item;
}
// update summary
QString sum( i18np( "Summary: %1 application found",
"Summary: %1 applications found", mFound ) );
mSummary->setText( sum );
}
// stop scanning
mProgress->setValue( 0 );
mProgress->setTextVisible( false );
enableButton(User1, true ); //enable scan button
if ( mFound > 0 ) {
enableButton(Apply, true );
enableButton(User2, true );
enableButton(User3, true );
}
}
void TopLevel::slotSelectAll()
{
AppLnkCache* cache;
Q_FOREACH( cache, mAppCache )
cache->item->setCheckState( 0, Qt::Checked );
}
void TopLevel::slotUnselectAll()
{
AppLnkCache* cache;
Q_FOREACH( cache, mAppCache )
cache->item->setCheckState( 0, Qt::Unchecked );
}
void TopLevel::slotCreate()
{
// copy template files
mAdded = 0;
createDesktopFiles( mAppCache, mAdded );
// decorate directories
decorateDirs( mDestDir );
KBuildSycocaProgressDialog::rebuildKSycoca(this);
QString message( i18np( "%1 application was added to the KDE menu system.",
"%1 applications were added to the KDE menu system.", mAdded ) );
KMessageBox::information( this, message, QString(), "ShowInformation" );
}
#include "toplevel.moc"
|