00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #include <qfile.h>
00025 #include <qdir.h>
00026 #include <qdialog.h>
00027 #include <qpixmap.h>
00028 #include <qlabel.h>
00029 #include <qlayout.h>
00030 #include <qpushbutton.h>
00031 #include <qtoolbutton.h>
00032 #include <qcheckbox.h>
00033 #include <qtooltip.h>
00034 #include <qstyle.h>
00035
00036 #include <kapplication.h>
00037 #include <kbuttonbox.h>
00038 #include <kcombobox.h>
00039 #include <kdesktopfile.h>
00040 #include <kdialog.h>
00041 #include <kglobal.h>
00042 #include <klineedit.h>
00043 #include <klocale.h>
00044 #include <kiconloader.h>
00045 #include <kmimemagic.h>
00046 #include <krun.h>
00047 #include <kstandarddirs.h>
00048 #include <kstringhandler.h>
00049 #include <kuserprofile.h>
00050 #include <kurlcompletion.h>
00051 #include <kurlrequester.h>
00052 #include <dcopclient.h>
00053 #include <kmimetype.h>
00054 #include <kservicegroup.h>
00055 #include <klistview.h>
00056 #include <ksycoca.h>
00057 #include <kdebug.h>
00058
00059 #include "kapplicationtree.h"
00060
00061 template class QPtrList<QString>;
00062
00063 #define SORT_SPEC (QDir::DirsFirst | QDir::Name | QDir::IgnoreCase)
00064
00065
00066
00067
00068 KDevAppTreeListItem::KDevAppTreeListItem( KListView* parent, const QString & name,
00069 const QPixmap& pixmap, bool parse, bool dir, const QString& p, const QString& c, const QString& dE )
00070 : QListViewItem( parent, name )
00071 {
00072 init(pixmap, parse, dir, p, c, dE);
00073 }
00074
00075
00076
00077
00078 KDevAppTreeListItem::KDevAppTreeListItem( QListViewItem* parent, const QString & name,
00079 const QPixmap& pixmap, bool parse, bool dir, const QString& p, const QString& c, const QString& dE )
00080 : QListViewItem( parent, name )
00081 {
00082 init(pixmap, parse, dir, p, c, dE);
00083 }
00084
00085
00086
00087
00088 void KDevAppTreeListItem::init(const QPixmap& pixmap, bool parse, bool dir, const QString& _path, const QString& _exec, const QString& _dEntry)
00089 {
00090 setPixmap(0, pixmap);
00091 parsed = parse;
00092 directory = dir;
00093 path = _path;
00094 exec = _exec;
00095 dEntry = _dEntry;
00096 exec.simplifyWhiteSpace();
00097 exec.truncate(exec.find(' '));
00098 }
00099
00100
00101
00102
00103
00104 QString KDevAppTreeListItem::key(int column, bool ) const
00105 {
00106 if (directory)
00107 return QString::fromLatin1(" ") + text(column).upper();
00108 else
00109 return text(column).upper();
00110 }
00111
00112 void KDevAppTreeListItem::activate()
00113 {
00114 if ( directory )
00115 setOpen(!isOpen());
00116 }
00117
00118 void KDevAppTreeListItem::setOpen( bool o )
00119 {
00120 if( o && !parsed ) {
00121 ((KDevApplicationTree *) parent())->addDesktopGroup( path, this );
00122 parsed = true;
00123 }
00124 QListViewItem::setOpen( o );
00125 }
00126
00127 bool KDevAppTreeListItem::isDirectory()
00128 {
00129 return directory;
00130 }
00131
00132
00133
00134 KDevApplicationTree::KDevApplicationTree( QWidget *parent, const char* name )
00135 : KListView( parent, name ), currentitem(0)
00136 {
00137 addColumn( i18n("Known Applications") );
00138 setRootIsDecorated( true );
00139
00140 addDesktopGroup( QString::null );
00141
00142 connect( this, SIGNAL( currentChanged(QListViewItem*) ), SLOT( slotItemHighlighted(QListViewItem*) ) );
00143 connect( this, SIGNAL( selectionChanged(QListViewItem*) ), SLOT( slotSelectionChanged(QListViewItem*) ) );
00144 }
00145
00146
00147
00148 bool KDevApplicationTree::isDirSel()
00149 {
00150 if (!currentitem) return false;
00151 return currentitem->isDirectory();
00152 }
00153
00154
00155
00156 void KDevApplicationTree::addDesktopGroup( QString relPath, KDevAppTreeListItem *item)
00157 {
00158 KServiceGroup::Ptr root = KServiceGroup::group(relPath);
00159 KServiceGroup::List list = root->entries();
00160
00161 KDevAppTreeListItem * newItem;
00162 for( KServiceGroup::List::ConstIterator it = list.begin();
00163 it != list.end(); it++)
00164 {
00165 QString icon;
00166 QString text;
00167 QString relPath;
00168 QString exec;
00169 QString dEntry;
00170 bool isDir = false;
00171 KSycocaEntry *p = (*it);
00172 if (p->isType(KST_KService))
00173 {
00174 KService *service = static_cast<KService *>(p);
00175 icon = service->icon();
00176 text = service->name();
00177 exec = service->exec();
00178 dEntry = service->desktopEntryPath();
00179 }
00180 else if (p->isType(KST_KServiceGroup))
00181 {
00182 KServiceGroup *serviceGroup = static_cast<KServiceGroup *>(p);
00183 icon = serviceGroup->icon();
00184 text = serviceGroup->caption();
00185 relPath = serviceGroup->relPath();
00186 isDir = true;
00187 if ( text[0] == '.' )
00188 continue;
00189
00190 KServiceGroup::Ptr subMenuRoot = KServiceGroup::group(relPath);
00191 if (subMenuRoot->childCount() == 0)
00192 continue;
00193 }
00194 else
00195 {
00196 kdWarning(250) << "KServiceGroup: Unexpected object in list!" << endl;
00197 continue;
00198 }
00199
00200 QPixmap pixmap = SmallIcon( icon );
00201
00202 if (item)
00203 newItem = new KDevAppTreeListItem( item, text, pixmap, false, isDir,
00204 relPath, exec, dEntry );
00205 else
00206 newItem = new KDevAppTreeListItem( this, text, pixmap, false, isDir,
00207 relPath, exec, dEntry );
00208 if (isDir)
00209 newItem->setExpandable( true );
00210 }
00211 }
00212
00213
00214
00215
00216 void KDevApplicationTree::slotItemHighlighted(QListViewItem* i)
00217 {
00218
00219 if(!i)
00220 return;
00221
00222 KDevAppTreeListItem *item = (KDevAppTreeListItem *) i;
00223
00224 currentitem = item;
00225
00226 if( (!item->directory ) && (!item->exec.isEmpty()) )
00227 emit highlighted( item->text(0), item->exec );
00228 }
00229
00230
00231
00232
00233 void KDevApplicationTree::slotSelectionChanged(QListViewItem* i)
00234 {
00235
00236 if(!i)
00237 return;
00238
00239 KDevAppTreeListItem *item = (KDevAppTreeListItem *) i;
00240
00241 currentitem = item;
00242
00243 if( ( !item->directory ) && (!item->exec.isEmpty() ) )
00244 emit selected( item->text(0), item->exec );
00245 }
00246
00247
00248
00249 void KDevApplicationTree::resizeEvent( QResizeEvent * e)
00250 {
00251 setColumnWidth(0, width()-QApplication::style().pixelMetric(QStyle::PM_ScrollBarExtent));
00252 KListView::resizeEvent(e);
00253 }
00254
00255
00256
00257 #include "kapplicationtree.moc"
00258