00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #include <qfile.h>
00024 #include <qdir.h>
00025 #include <qdialog.h>
00026 #include <qimage.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 #include <qwhatsthis.h>
00036
00037 #include <kapplication.h>
00038 #include <kbuttonbox.h>
00039 #include <kcombobox.h>
00040 #include <kdesktopfile.h>
00041 #include <kdialog.h>
00042 #include <kglobal.h>
00043 #include <klineedit.h>
00044 #include <klocale.h>
00045 #include <kiconloader.h>
00046 #include <kmimemagic.h>
00047 #include <krun.h>
00048 #include <kstandarddirs.h>
00049 #include <kstringhandler.h>
00050 #include <kuserprofile.h>
00051 #include <kurlcompletion.h>
00052 #include <kurlrequester.h>
00053 #include <dcopclient.h>
00054 #include <kmimetype.h>
00055 #include <kservicegroup.h>
00056 #include <klistview.h>
00057 #include <ksycoca.h>
00058 #include <kstdguiitem.h>
00059
00060 #include "kopenwith.h"
00061 #include "kopenwith_p.h"
00062
00063 #include <kdebug.h>
00064 #include <assert.h>
00065 #include <stdlib.h>
00066
00067 template class QPtrList<QString>;
00068
00069 #define SORT_SPEC (QDir::DirsFirst | QDir::Name | QDir::IgnoreCase)
00070
00071
00072
00073
00074 KAppTreeListItem::KAppTreeListItem( KListView* parent, const QString & name,
00075 const QPixmap& pixmap, bool parse, bool dir, const QString &p, const QString &c )
00076 : QListViewItem( parent, name )
00077 {
00078 init(pixmap, parse, dir, p, c);
00079 }
00080
00081
00082
00083
00084 KAppTreeListItem::KAppTreeListItem( QListViewItem* parent, const QString & name,
00085 const QPixmap& pixmap, bool parse, bool dir, const QString &p, const QString &c )
00086 : QListViewItem( parent, name )
00087 {
00088 init(pixmap, parse, dir, p, c);
00089 }
00090
00091
00092
00093
00094 void KAppTreeListItem::init(const QPixmap& pixmap, bool parse, bool dir, const QString &_path, const QString &_exec)
00095 {
00096 setPixmap(0, pixmap);
00097 parsed = parse;
00098 directory = dir;
00099 path = _path;
00100 exec = _exec;
00101 }
00102
00103
00104
00105 int KAppTreeListItem::compare(QListViewItem *i, int col, bool ascending) const
00106 {
00107 KAppTreeListItem *other = dynamic_cast<KAppTreeListItem *>(i);
00108
00109
00110 if (directory && !other->directory)
00111 return -1;
00112
00113 else if (!directory && other->directory)
00114 return 1;
00115
00116 else
00117 return QListViewItem::compare(i, col, ascending);
00118 }
00119
00120
00121
00122 QString KAppTreeListItem::key(int column, bool ) const
00123 {
00124 return text(column).upper();
00125 }
00126
00127 void KAppTreeListItem::activate()
00128 {
00129 if ( directory )
00130 setOpen(!isOpen());
00131 }
00132
00133 void KAppTreeListItem::setOpen( bool o )
00134 {
00135 if( o && !parsed ) {
00136 ((KApplicationTree *) parent())->addDesktopGroup( path, this );
00137 parsed = true;
00138 }
00139 QListViewItem::setOpen( o );
00140 }
00141
00142 bool KAppTreeListItem::isDirectory()
00143 {
00144 return directory;
00145 }
00146
00147
00148
00149 KApplicationTree::KApplicationTree( QWidget *parent )
00150 : KListView( parent ), currentitem(0)
00151 {
00152 addColumn( i18n("Known Applications") );
00153 setRootIsDecorated( true );
00154
00155 addDesktopGroup( QString::null );
00156 cleanupTree();
00157
00158 connect( this, SIGNAL( currentChanged(QListViewItem*) ),
00159 SLOT( slotItemHighlighted(QListViewItem*) ) );
00160 connect( this, SIGNAL( selectionChanged(QListViewItem*) ),
00161 SLOT( slotSelectionChanged(QListViewItem*) ) );
00162 }
00163
00164
00165
00166 bool KApplicationTree::isDirSel()
00167 {
00168 if (!currentitem) return false;
00169 return currentitem->isDirectory();
00170 }
00171
00172
00173
00174 static QPixmap appIcon(const QString &iconName)
00175 {
00176 QPixmap normal = KGlobal::iconLoader()->loadIcon(iconName, KIcon::Small, 0, KIcon::DefaultState, 0L, true);
00177
00178 if (normal.width() > 20 || normal.height() > 20)
00179 {
00180 QImage tmp = normal.convertToImage();
00181 tmp = tmp.smoothScale(20, 20);
00182 normal.convertFromImage(tmp);
00183 }
00184 return normal;
00185 }
00186
00187 void KApplicationTree::addDesktopGroup( const QString &relPath, KAppTreeListItem *item)
00188 {
00189 KServiceGroup::Ptr root = KServiceGroup::group(relPath);
00190 if (!root || !root->isValid()) return;
00191
00192 KServiceGroup::List list = root->entries();
00193
00194 KAppTreeListItem * newItem;
00195 for( KServiceGroup::List::ConstIterator it = list.begin();
00196 it != list.end(); it++)
00197 {
00198 QString icon;
00199 QString text;
00200 QString relPath;
00201 QString exec;
00202 bool isDir = false;
00203 KSycocaEntry *p = (*it);
00204 if (p->isType(KST_KService))
00205 {
00206 KService *service = static_cast<KService *>(p);
00207
00208 if (service->noDisplay())
00209 continue;
00210
00211 icon = service->icon();
00212 text = service->name();
00213 exec = service->exec();
00214 }
00215 else if (p->isType(KST_KServiceGroup))
00216 {
00217 KServiceGroup *serviceGroup = static_cast<KServiceGroup *>(p);
00218
00219 if (serviceGroup->noDisplay())
00220 continue;
00221
00222 icon = serviceGroup->icon();
00223 text = serviceGroup->caption();
00224 relPath = serviceGroup->relPath();
00225 isDir = true;
00226 if ( text[0] == '.' )
00227 continue;
00228 }
00229 else
00230 {
00231 kdWarning(250) << "KServiceGroup: Unexpected object in list!" << endl;
00232 continue;
00233 }
00234
00235 QPixmap pixmap = appIcon( icon );
00236
00237 if (item)
00238 newItem = new KAppTreeListItem( item, text, pixmap, false, isDir,
00239 relPath, exec );
00240 else
00241 newItem = new KAppTreeListItem( this, text, pixmap, false, isDir,
00242 relPath, exec );
00243 if (isDir)
00244 newItem->setExpandable( true );
00245 }
00246 }
00247
00248
00249
00250
00251 void KApplicationTree::slotItemHighlighted(QListViewItem* i)
00252 {
00253
00254 if(!i)
00255 return;
00256
00257 KAppTreeListItem *item = (KAppTreeListItem *) i;
00258
00259 currentitem = item;
00260
00261 if( (!item->directory ) && (!item->exec.isEmpty()) )
00262 emit highlighted( item->text(0), item->exec );
00263 }
00264
00265
00266
00267
00268 void KApplicationTree::slotSelectionChanged(QListViewItem* i)
00269 {
00270
00271 if(!i)
00272 return;
00273
00274 KAppTreeListItem *item = (KAppTreeListItem *) i;
00275
00276 currentitem = item;
00277
00278 if( ( !item->directory ) && (!item->exec.isEmpty() ) )
00279 emit selected( item->text(0), item->exec );
00280 }
00281
00282
00283
00284 void KApplicationTree::resizeEvent( QResizeEvent * e)
00285 {
00286 setColumnWidth(0, width()-QApplication::style().pixelMetric(QStyle::PM_ScrollBarExtent)
00287 -2*QApplication::style().pixelMetric(QStyle::PM_DefaultFrameWidth));
00288 KListView::resizeEvent(e);
00289 }
00290
00291
00292 void KApplicationTree::cleanupTree()
00293 {
00294 QListViewItem *item=firstChild();
00295 while(item!=0)
00296 {
00297 if(item->isExpandable())
00298 {
00299 item->setOpen(true);
00300 if(item->childCount()==0) {
00301 QListViewItem *current=item;
00302 item=item->itemBelow();
00303 delete current;
00304 continue;
00305 }
00306 item=item->itemBelow();
00307 continue;
00308 }
00309 item=item->itemBelow();
00310 }
00311 item=firstChild();
00312 while(item!=0)
00313 {
00314 if(item->isExpandable())
00315 {
00316 QListViewItem *temp=item->itemBelow();
00317 if(item->text(0)!=i18n("Applications"))
00318 item->setOpen(false);
00319 item=temp;
00320 continue;
00321 }
00322 item=item->itemBelow();
00323 }
00324 }
00325
00326
00327
00328
00329
00330
00331 class KOpenWithDlgPrivate
00332 {
00333 public:
00334 KOpenWithDlgPrivate() : saveNewApps(false) { };
00335 QPushButton* ok;
00336 bool saveNewApps;
00337 KService::Ptr curService;
00338 };
00339
00340 KOpenWithDlg::KOpenWithDlg( const KURL::List& _urls, QWidget* parent )
00341 :QDialog( parent, "openwith", true )
00342 {
00343 setCaption( i18n( "Open With" ) );
00344 QString text;
00345 if( _urls.count() == 1 )
00346 {
00347 text = i18n("<qt>Select the program that should be used to open <b>%1</b>. "
00348 "If the program is not listed, enter the name or click "
00349 "the browse button.</qt>").arg( _urls.first().fileName() );
00350 }
00351 else
00352
00353 text = i18n( "Choose the name of the program with which to open the selected files." );
00354 setServiceType( _urls );
00355 init( text, QString() );
00356 }
00357
00358 KOpenWithDlg::KOpenWithDlg( const KURL::List& _urls, const QString&_text,
00359 const QString& _value, QWidget *parent)
00360 :QDialog( parent, "openwith", true )
00361 {
00362 QString caption = KStringHandler::csqueeze( _urls.first().prettyURL() );
00363 if (_urls.count() > 1)
00364 caption += QString::fromLatin1("...");
00365 setCaption(caption);
00366 setServiceType( _urls );
00367 init( _text, _value );
00368 }
00369
00370 KOpenWithDlg::KOpenWithDlg( const QString &serviceType, const QString& value,
00371 QWidget *parent)
00372 :QDialog( parent, "openwith", true )
00373 {
00374 setCaption(i18n("Choose Application for %1").arg(serviceType));
00375 QString text = i18n("<qt>Select the program for the file type: <b>%1</b>. "
00376 "If the program is not listed, enter the name or click "
00377 "the browse button.</qt>").arg(serviceType);
00378 qServiceType = serviceType;
00379 init( text, value );
00380 if (remember)
00381 remember->hide();
00382 }
00383
00384 KOpenWithDlg::KOpenWithDlg( QWidget *parent)
00385 :QDialog( parent, "openwith", true )
00386 {
00387 setCaption(i18n("Choose Application"));
00388 QString text = i18n("<qt>Select a program. "
00389 "If the program is not listed, enter the name or click "
00390 "the browse button.</qt>");
00391 qServiceType = QString::null;
00392 init( text, QString::null );
00393 }
00394
00395 void KOpenWithDlg::setServiceType( const KURL::List& _urls )
00396 {
00397 if ( _urls.count() == 1 )
00398 {
00399 qServiceType = KMimeType::findByURL( _urls.first())->name();
00400 if (qServiceType == QString::fromLatin1("application/octet-stream"))
00401 qServiceType = QString::null;
00402 }
00403 else
00404 qServiceType = QString::null;
00405 }
00406
00407 void KOpenWithDlg::init( const QString& _text, const QString& _value )
00408 {
00409 d = new KOpenWithDlgPrivate;
00410 bool bReadOnly = kapp && !kapp->authorize("shell_access");
00411 m_terminaldirty = false;
00412 m_pTree = 0L;
00413 m_pService = 0L;
00414 d->curService = 0L;
00415
00416 QBoxLayout *topLayout = new QVBoxLayout( this, KDialog::marginHint(),
00417 KDialog::spacingHint() );
00418 label = new QLabel( _text, this );
00419 topLayout->addWidget(label);
00420
00421 QHBoxLayout* hbox = new QHBoxLayout(topLayout);
00422
00423 QToolButton *clearButton = new QToolButton( this );
00424 clearButton->setIconSet( BarIcon( "locationbar_erase" ) );
00425 clearButton->setFixedSize( clearButton->sizeHint() );
00426 connect( clearButton, SIGNAL( clicked() ), SLOT( slotClear() ) );
00427 QToolTip::add( clearButton, i18n( "Clear input field" ) );
00428
00429 hbox->addWidget( clearButton );
00430
00431 if (!bReadOnly)
00432 {
00433
00434 KHistoryCombo *combo = new KHistoryCombo();
00435 combo->setDuplicatesEnabled( false );
00436 KConfig *kc = KGlobal::config();
00437 KConfigGroupSaver ks( kc, QString::fromLatin1("Open-with settings") );
00438 int max = kc->readNumEntry( QString::fromLatin1("Maximum history"), 15 );
00439 combo->setMaxCount( max );
00440 int mode = kc->readNumEntry(QString::fromLatin1("CompletionMode"),
00441 KGlobalSettings::completionMode());
00442 combo->setCompletionMode((KGlobalSettings::Completion)mode);
00443 QStringList list = kc->readListEntry( QString::fromLatin1("History") );
00444 combo->setHistoryItems( list, true );
00445 edit = new KURLRequester( combo, this );
00446 }
00447 else
00448 {
00449 clearButton->hide();
00450 edit = new KURLRequester( this );
00451 edit->lineEdit()->setReadOnly(true);
00452 edit->button()->hide();
00453 }
00454
00455 edit->setURL( _value );
00456 QWhatsThis::add(edit,i18n(
00457 "Following the command, you can have several place holders which will be replaced "
00458 "with the actual values when the actual program is run:\n"
00459 "%f - a single file name\n"
00460 "%F - a list of files; use for applications that can open several local files at once\n"
00461 "%u - a single URL\n"
00462 "%U - a list of URLs\n"
00463 "%d - the directory of the file to open\n"
00464 "%D - a list of directories\n"
00465 "%i - the icon\n"
00466 "%m - the mini-icon\n"
00467 "%c - the comment"));
00468
00469 hbox->addWidget(edit);
00470
00471 if ( edit->comboBox() ) {
00472 KURLCompletion *comp = new KURLCompletion( KURLCompletion::ExeCompletion );
00473 edit->comboBox()->setCompletionObject( comp );
00474 edit->comboBox()->setAutoDeleteCompletionObject( true );
00475 }
00476
00477 connect ( edit, SIGNAL(returnPressed()), SLOT(slotOK()) );
00478 connect ( edit, SIGNAL(textChanged(const QString&)), SLOT(slotTextChanged()) );
00479
00480 m_pTree = new KApplicationTree( this );
00481 topLayout->addWidget(m_pTree);
00482
00483 connect( m_pTree, SIGNAL( selected( const QString&, const QString& ) ),
00484 SLOT( slotSelected( const QString&, const QString& ) ) );
00485 connect( m_pTree, SIGNAL( highlighted( const QString&, const QString& ) ),
00486 SLOT( slotHighlighted( const QString&, const QString& ) ) );
00487 connect( m_pTree, SIGNAL( doubleClicked(QListViewItem*) ),
00488 SLOT( slotDbClick() ) );
00489
00490 terminal = new QCheckBox( i18n("Run in &terminal"), this );
00491 if (bReadOnly)
00492 terminal->hide();
00493 connect(terminal, SIGNAL(toggled(bool)), SLOT(slotTerminalToggled(bool)));
00494
00495 topLayout->addWidget(terminal);
00496
00497 QBoxLayout* nocloseonexitLayout = new QHBoxLayout( 0, 0, KDialog::spacingHint() );
00498 QSpacerItem* spacer = new QSpacerItem( 20, 0, QSizePolicy::Fixed, QSizePolicy::Minimum );
00499 nocloseonexitLayout->addItem( spacer );
00500
00501 nocloseonexit = new QCheckBox( i18n("&Do not close when command exits"), this );
00502 nocloseonexit->setChecked( false );
00503 nocloseonexit->setDisabled( true );
00504
00505
00506
00507 KConfigGroup confGroup( KGlobal::config(), QString::fromLatin1("General") );
00508 QString preferredTerminal = confGroup.readPathEntry("TerminalApplication", QString::fromLatin1("konsole"));
00509
00510 if (bReadOnly || preferredTerminal != "konsole")
00511 nocloseonexit->hide();
00512
00513 nocloseonexitLayout->addWidget( nocloseonexit );
00514 topLayout->addLayout( nocloseonexitLayout );
00515
00516 if (!qServiceType.isNull())
00517 {
00518 remember = new QCheckBox(i18n("&Remember application association for this type of file"), this);
00519
00520 topLayout->addWidget(remember);
00521 }
00522 else
00523 remember = 0L;
00524
00525
00526 KButtonBox* b = new KButtonBox( this );
00527 b->addStretch( 2 );
00528
00529 d->ok = b->addButton( KStdGuiItem::ok() );
00530 d->ok->setDefault( true );
00531 connect( d->ok, SIGNAL( clicked() ), SLOT( slotOK() ) );
00532
00533 QPushButton* cancel = b->addButton( KStdGuiItem::cancel() );
00534 connect( cancel, SIGNAL( clicked() ), SLOT( reject() ) );
00535
00536 b->layout();
00537 topLayout->addWidget( b );
00538
00539
00540
00541
00542
00543 edit->setFocus();
00544 slotTextChanged();
00545 }
00546
00547
00548
00549
00550 KOpenWithDlg::~KOpenWithDlg()
00551 {
00552 delete d;
00553 d = 0;
00554 }
00555
00556
00557
00558 void KOpenWithDlg::slotClear()
00559 {
00560 edit->setURL(QString::null);
00561 edit->setFocus();
00562 }
00563
00564
00565
00566
00567 void KOpenWithDlg::slotSelected( const QString& , const QString& _exec )
00568 {
00569 kdDebug(250)<<"KOpenWithDlg::slotSelected"<<endl;
00570 KService::Ptr pService = d->curService;
00571 edit->setURL( _exec );
00572 d->curService = pService;
00573 }
00574
00575
00576
00577
00578 void KOpenWithDlg::slotHighlighted( const QString& _name, const QString& )
00579 {
00580 kdDebug(250)<<"KOpenWithDlg::slotHighlighted"<<endl;
00581 qName = _name;
00582 d->curService = KService::serviceByName( qName );
00583 if (!m_terminaldirty)
00584 {
00585
00586 terminal->setChecked(d->curService->terminal());
00587 QString terminalOptions = d->curService->terminalOptions();
00588 nocloseonexit->setChecked( (terminalOptions.contains( "--noclose" ) > 0) );
00589 m_terminaldirty = false;
00590 }
00591 }
00592
00593
00594
00595 void KOpenWithDlg::slotTextChanged()
00596 {
00597 kdDebug(250)<<"KOpenWithDlg::slotTextChanged"<<endl;
00598
00599 d->curService = 0L;
00600 d->ok->setEnabled( !edit->url().isEmpty());
00601 }
00602
00603
00604
00605 void KOpenWithDlg::slotTerminalToggled(bool)
00606 {
00607
00608 m_terminaldirty = true;
00609 nocloseonexit->setDisabled( ! terminal->isChecked() );
00610 }
00611
00612
00613
00614 void KOpenWithDlg::slotDbClick()
00615 {
00616 if (m_pTree->isDirSel() ) return;
00617 slotOK();
00618 }
00619
00620 void KOpenWithDlg::setSaveNewApplications(bool b)
00621 {
00622 d->saveNewApps = b;
00623 }
00624
00625 void KOpenWithDlg::slotOK()
00626 {
00627 QString typedExec(edit->url());
00628 QString fullExec(typedExec);
00629
00630 QString serviceName;
00631 QString initialServiceName;
00632 QString preferredTerminal;
00633 m_pService = d->curService;
00634 if (!m_pService) {
00635
00636
00637
00638 serviceName = KRun::binaryName( typedExec, true );
00639 if (serviceName.isEmpty())
00640 {
00641
00642 return;
00643 }
00644 initialServiceName = serviceName;
00645 kdDebug(250) << "initialServiceName=" << initialServiceName << endl;
00646 int i = 1;
00647 bool ok = false;
00648
00649 do {
00650 kdDebug(250) << "looking for service " << serviceName << endl;
00651 KService::Ptr serv = KService::serviceByDesktopName( serviceName );
00652 ok = !serv;
00653
00654 if ( serv && serv->type() == "Application")
00655 {
00656 QString exec = serv->exec();
00657 fullExec = exec;
00658 exec.replace("%u", "", false);
00659 exec.replace("%f", "", false);
00660 exec.replace("-caption %c", "");
00661 exec.replace("-caption \"%c\"", "");
00662 exec.replace("%i", "");
00663 exec.replace("%m", "");
00664 exec = exec.simplifyWhiteSpace();
00665 if (exec == typedExec)
00666 {
00667 ok = true;
00668 m_pService = serv;
00669 kdDebug(250) << k_funcinfo << "OK, found identical service: " << serv->desktopEntryPath() << endl;
00670 }
00671 }
00672 if (!ok)
00673 {
00674 ++i;
00675 serviceName = initialServiceName + "-" + QString::number(i);
00676 }
00677 }
00678 while (!ok);
00679 }
00680 if ( m_pService )
00681 {
00682
00683 serviceName = m_pService->name();
00684 initialServiceName = serviceName;
00685 fullExec = m_pService->exec();
00686 }
00687
00688 if (terminal->isChecked())
00689 {
00690 KConfigGroup confGroup( KGlobal::config(), QString::fromLatin1("General") );
00691 preferredTerminal = confGroup.readPathEntry("TerminalApplication", QString::fromLatin1("konsole"));
00692 m_command = preferredTerminal;
00693
00694 if (preferredTerminal == "konsole" && nocloseonexit->isChecked())
00695 m_command += QString::fromLatin1(" --noclose");
00696 m_command += QString::fromLatin1(" -e ");
00697 m_command += edit->url();
00698 kdDebug(250) << "Setting m_command to " << m_command << endl;
00699 }
00700 if ( m_pService && terminal->isChecked() != m_pService->terminal() )
00701 m_pService = 0L;
00702
00703 bool bRemember = remember && remember->isChecked();
00704
00705 if ( !bRemember && m_pService)
00706 {
00707 accept();
00708 return;
00709 }
00710
00711 if (!bRemember && !d->saveNewApps)
00712 {
00713
00714 m_pService = new KService(initialServiceName, fullExec, QString::null);
00715 if (terminal->isChecked())
00716 {
00717 m_pService->setTerminal(true);
00718
00719 if (preferredTerminal == "konsole" && nocloseonexit->isChecked())
00720 m_pService->setTerminalOptions("--noclose");
00721 }
00722 accept();
00723 return;
00724 }
00725
00726
00727
00728
00729
00730 QString newPath;
00731 QString oldPath;
00732 QString menuId;
00733 if (m_pService)
00734 {
00735 oldPath = m_pService->desktopEntryPath();
00736 newPath = m_pService->locateLocal();
00737 menuId = m_pService->menuId();
00738 kdDebug(250) << "Updating exitsing service " << m_pService->desktopEntryPath() << " ( " << newPath << " ) " << endl;
00739 }
00740 else
00741 {
00742 newPath = KService::newServicePath(false , serviceName, &menuId);
00743 kdDebug(250) << "Creating new service " << serviceName << " ( " << newPath << " ) " << endl;
00744 }
00745
00746 int maxPreference = 1;
00747 if (!qServiceType.isEmpty())
00748 {
00749 KServiceTypeProfile::OfferList offerList = KServiceTypeProfile::offers( qServiceType );
00750 if (!offerList.isEmpty())
00751 maxPreference = offerList.first().preference();
00752 }
00753 QString menuElement;
00754 menuElement="?package(menu): needs=\"x11\" section=\".hidden\" ";
00755 KDesktopFile *desktop = 0;
00756 if (!oldPath.isEmpty() && (oldPath != newPath))
00757 {
00758 KDesktopFile orig(oldPath, true);
00759 if ( m_pService )
00760 desktop = orig.copyTo( pathToCopyEntry(newPath, m_pService->menuId() ) );
00761 else
00762 desktop = orig.copyTo(newPath);
00763 }
00764 else
00765 {
00766 if ( m_pService )
00767 desktop = new KDesktopFile(pathToCopyEntry(newPath, m_pService->menuId() ) );
00768 else
00769 desktop = new KDesktopFile(newPath);
00770 }
00771 desktop->writeEntry("Type", QString::fromLatin1("Application"));
00772 desktop->writeEntry("Name", initialServiceName);
00773 desktop->writePathEntry("Exec", fullExec);
00774 menuElement+="command=\"";
00775 menuElement+=fullExec+"\" ";
00776 menuElement+="title=\"";
00777 menuElement+=initialServiceName+"\" ";
00778 QString kdeOpt = "kde_opt=\"";
00779 if (terminal->isChecked())
00780 {
00781 desktop->writeEntry("Terminal", true);
00782 kdeOpt+="Terminal=1\\\\n";
00783
00784 if (preferredTerminal == "konsole" && nocloseonexit->isChecked())
00785 {
00786 desktop->writeEntry("TerminalOptions", "--noclose");
00787 kdeOpt+=" TerminalOptions=--noclose\\\\n";
00788 }
00789 }
00790 else
00791 {
00792 desktop->writeEntry("Terminal", false);
00793 }
00794 desktop->writeEntry("InitialPreference", maxPreference + 1);
00795 kdeOpt+=QString( "InitialPreference=%1\\\\n" ).arg( maxPreference + 1 );
00796
00797 if (bRemember || d->saveNewApps)
00798 {
00799 QStringList mimeList = desktop->readListEntry("MimeType", ';');
00800 if (!qServiceType.isEmpty() && !mimeList.contains(qServiceType))
00801 mimeList.append(qServiceType);
00802 desktop->writeEntry("MimeType", mimeList, ';');
00803 menuElement+=QString( "mimetypes=\"%1\"" ).arg( mimeList.join( ";") );
00804 if ( !qServiceType.isEmpty() )
00805 {
00806
00807 KDesktopFile mimeDesktop( locateLocal( "mime", qServiceType + ".desktop" ) );
00808 mimeDesktop.writeEntry( "X-KDE-AutoEmbed", false );
00809 mimeDesktop.sync();
00810 }
00811 }
00812
00813 if ( newPath.contains( "Mandrakelinux/.hidden" ) && ( KStandardDirs::menu_type_by_version()!="kde" ))
00814 {
00815 QStringList categorieList = desktop->readListEntry("Categories", ';');
00816 if ( !categorieList.contains("X-Mandrakelinux-.hidden"))
00817 categorieList.append("X-Mandrakelinux-.hidden");
00818 desktop->writeEntry( "Categories", categorieList, ";" );
00819
00820 menuElement+=" " +kdeOpt+"\"";
00821 saveInMDKMenuStructure( initialServiceName,menuElement );
00822 }
00823
00824 desktop->sync();
00825 delete desktop;
00826
00827 KService::rebuildKSycoca(this);
00828
00829 m_pService = KService::serviceByMenuId( menuId );
00830
00831 Q_ASSERT( m_pService );
00832
00833 accept();
00834 }
00835
00836 void KOpenWithDlg::saveInMDKMenuStructure( const QString & filename, const QString &menuElement )
00837 {
00838 QString menuPath;
00839 QString tmp =KStandardDirs::menu_type_by_version();
00840 if( tmp=="kde")
00841 return;
00842 else if ( tmp=="mdk" )
00843 {
00844 menuPath=".menu/";
00845 }
00846 else if ( tmp == "mdk-simplified" )
00847 {
00848 menuPath=".menu-simplified/";
00849 }
00850 else
00851 {
00852 kdDebug()<<" Error in type of menu\n";
00853 return;
00854 }
00855 QFile menuStamp(QDir::homeDirPath ()+"/.menu-updates.stamp");
00856 if( menuStamp.exists())
00857 menuStamp.remove();
00858 QDir dir;
00859 dir.mkdir( QDir::homeDirPath ()+"/"+menuPath );
00860 QFile saveMenuMDK( QDir::homeDirPath ()+"/"+menuPath+filename );
00861 if ( saveMenuMDK.open( IO_WriteOnly ) ) {
00862 QTextStream stream( &saveMenuMDK );
00863 stream<<menuElement<<"\n";
00864 saveMenuMDK.close();
00865 }
00866 }
00867
00868 QString KOpenWithDlg::pathToCopyEntry( QString path, QString menuId )
00869 {
00870 if( KStandardDirs::menu_type_by_version()=="kde")
00871 return path;
00872 QString tmpPath = path.remove( menuId );
00873 QString element;
00874 element = menuId.remove( "Mandrakelinux-" );
00875 element=element.replace( QChar( '-' ), "/" );
00876 QString newPath = tmpPath+"Mandrakelinux/"+element;
00877 KURL url(newPath);
00878 KStandardDirs::makeDir( url.directory() );
00879 return newPath;
00880 }
00881
00882 QString KOpenWithDlg::text() const
00883 {
00884 if (!m_command.isEmpty())
00885 return m_command;
00886 else
00887 return edit->url();
00888 }
00889
00890 void KOpenWithDlg::hideNoCloseOnExit()
00891 {
00892
00893 nocloseonexit->setChecked( false );
00894 nocloseonexit->hide();
00895 }
00896
00897 void KOpenWithDlg::hideRunInTerminal()
00898 {
00899 terminal->hide();
00900 hideNoCloseOnExit();
00901 }
00902
00903 void KOpenWithDlg::accept()
00904 {
00905 KHistoryCombo *combo = static_cast<KHistoryCombo*>( edit->comboBox() );
00906 if ( combo ) {
00907 combo->addToHistory( edit->url() );
00908
00909 KConfig *kc = KGlobal::config();
00910 KConfigGroupSaver ks( kc, QString::fromLatin1("Open-with settings") );
00911 kc->writeEntry( QString::fromLatin1("History"), combo->historyItems() );
00912 kc->writeEntry(QString::fromLatin1("CompletionMode"),
00913 combo->completionMode());
00914
00915
00916 kc->sync();
00917 }
00918
00919 QDialog::accept();
00920 }
00921
00922
00924
00925 #ifndef KDE_NO_COMPAT
00926 bool KFileOpenWithHandler::displayOpenWithDialog( const KURL::List& urls )
00927 {
00928 KOpenWithDlg l( urls, i18n("Open with:"), QString::null, 0L );
00929 if ( l.exec() )
00930 {
00931 KService::Ptr service = l.service();
00932 if ( !!service )
00933 return KRun::run( *service, urls );
00934
00935 kdDebug(250) << "No service set, running " << l.text() << endl;
00936 return KRun::run( l.text(), urls );
00937 }
00938 return false;
00939 }
00940 #endif
00941
00942 #include "kopenwith.moc"
00943 #include "kopenwith_p.moc"
00944