• Skip to content
  • Skip to link menu
Trinity API Reference
  • Trinity API Reference
  • tdeio/tdefile
 

tdeio/tdefile

  • tdeio
  • tdefile
kopenwith.cpp
1 /* This file is part of the KDE libraries
2 
3  Copyright (C) 1997 Torben Weis <weis@stud.uni-frankfurt.de>
4  Copyright (C) 1999 Dirk Mueller <mueller@kde.org>
5  Portions copyright (C) 1999 Preston Brown <pbrown@kde.org>
6 
7  This library is free software; you can redistribute it and/or
8  modify it under the terms of the GNU Library General Public
9  License as published by the Free Software Foundation; either
10  version 2 of the License, or (at your option) any later version.
11 
12  This library is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  Library General Public License for more details.
16 
17  You should have received a copy of the GNU Library General Public License
18  along with this library; see the file COPYING.LIB. If not, write to
19  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20  Boston, MA 02110-1301, USA.
21 */
22 
23 #include <tqfile.h>
24 #include <tqdir.h>
25 #include <tqdialog.h>
26 #include <tqimage.h>
27 #include <tqpixmap.h>
28 #include <tqlabel.h>
29 #include <tqlayout.h>
30 #include <tqpushbutton.h>
31 #include <tqtoolbutton.h>
32 #include <tqcheckbox.h>
33 #include <tqtooltip.h>
34 #include <tqstyle.h>
35 #include <tqwhatsthis.h>
36 
37 #include <tdeapplication.h>
38 #include <kbuttonbox.h>
39 #include <kcombobox.h>
40 #include <kdesktopfile.h>
41 #include <kdialog.h>
42 #include <tdeglobal.h>
43 #include <klineedit.h>
44 #include <tdelocale.h>
45 #include <kiconloader.h>
46 #include <kmimemagic.h>
47 #include <krun.h>
48 #include <kstandarddirs.h>
49 #include <kstringhandler.h>
50 #include <kuserprofile.h>
51 #include <kurlcompletion.h>
52 #include <kurlrequester.h>
53 #include <dcopclient.h>
54 #include <kmimetype.h>
55 #include <kservicegroup.h>
56 #include <tdelistview.h>
57 #include <tdesycoca.h>
58 #include <kstdguiitem.h>
59 
60 #include "kopenwith.h"
61 #include "kopenwith_p.h"
62 
63 #include <kdebug.h>
64 #include <assert.h>
65 #include <stdlib.h>
66 
67 #define SORT_SPEC (TQDir::DirsFirst | TQDir::Name | TQDir::IgnoreCase)
68 
69 
70 // ----------------------------------------------------------------------
71 
72 KAppTreeListItem::KAppTreeListItem( TDEListView* parent, const TQString & name,
73  const TQPixmap& pixmap, bool parse, bool dir, const TQString &p, const TQString &c )
74  : TQListViewItem( parent, name )
75 {
76  init(pixmap, parse, dir, p, c);
77 }
78 
79 
80 // ----------------------------------------------------------------------
81 
82 KAppTreeListItem::KAppTreeListItem( TQListViewItem* parent, const TQString & name,
83  const TQPixmap& pixmap, bool parse, bool dir, const TQString &p, const TQString &c )
84  : TQListViewItem( parent, name )
85 {
86  init(pixmap, parse, dir, p, c);
87 }
88 
89 
90 // ----------------------------------------------------------------------
91 
92 void KAppTreeListItem::init(const TQPixmap& pixmap, bool parse, bool dir, const TQString &_path, const TQString &_exec)
93 {
94  setPixmap(0, pixmap);
95  parsed = parse;
96  directory = dir;
97  path = _path; // relative path
98  exec = _exec;
99 }
100 
101 
102 /* Ensures that directories sort before non-directories */
103 int KAppTreeListItem::compare(TQListViewItem *i, int col, bool ascending) const
104 {
105  KAppTreeListItem *other = dynamic_cast<KAppTreeListItem *>(i);
106 
107  // Directories sort first
108  if (directory && !other->directory)
109  return -1;
110 
111  else if (!directory && other->directory)
112  return 1;
113 
114  else // both directories or both not
115  return TQListViewItem::compare(i, col, ascending);
116 }
117 
118 // ----------------------------------------------------------------------
119 // Ensure that case is ignored
120 TQString KAppTreeListItem::key(int column, bool /*ascending*/) const
121 {
122  return text(column).upper();
123 }
124 
125 void KAppTreeListItem::activate()
126 {
127  if ( directory )
128  setOpen(!isOpen());
129 }
130 
131 void KAppTreeListItem::setOpen( bool o )
132 {
133  if( o && !parsed ) { // fill the children before opening
134  ((TDEApplicationTree *) parent())->addDesktopGroup( path, this );
135  parsed = true;
136  }
137  TQListViewItem::setOpen( o );
138 }
139 
140 bool KAppTreeListItem::isDirectory()
141 {
142  return directory;
143 }
144 
145 // ----------------------------------------------------------------------
146 
147 TDEApplicationTree::TDEApplicationTree( TQWidget *parent )
148  : TDEListView( parent ), currentitem(0)
149 {
150  addColumn( i18n("Known Applications") );
151  setRootIsDecorated( true );
152 
153  addDesktopGroup( TQString::null );
154  cleanupTree();
155 
156  connect( this, TQT_SIGNAL( currentChanged(TQListViewItem*) ),
157  TQT_SLOT( slotItemHighlighted(TQListViewItem*) ) );
158  connect( this, TQT_SIGNAL( selectionChanged(TQListViewItem*) ),
159  TQT_SLOT( slotSelectionChanged(TQListViewItem*) ) );
160 }
161 
162 // ----------------------------------------------------------------------
163 
164 bool TDEApplicationTree::isDirSel()
165 {
166  if (!currentitem) return false; // if currentitem isn't set
167  return currentitem->isDirectory();
168 }
169 
170 // ----------------------------------------------------------------------
171 
172 static TQPixmap appIcon(const TQString &iconName)
173 {
174  TQPixmap normal = TDEGlobal::iconLoader()->loadIcon(iconName, TDEIcon::Small, 0, TDEIcon::DefaultState, 0L, true);
175  // make sure they are not larger than 20x20
176  if (normal.width() > 20 || normal.height() > 20)
177  {
178  TQImage tmp = normal.convertToImage();
179  tmp = tmp.smoothScale(20, 20);
180  normal.convertFromImage(tmp);
181  }
182  return normal;
183 }
184 
185 void TDEApplicationTree::addDesktopGroup( const TQString &relPath, KAppTreeListItem *item)
186 {
187  KServiceGroup::Ptr root = KServiceGroup::group(relPath);
188  if (!root || !root->isValid()) return;
189 
190  KServiceGroup::List list = root->entries();
191 
192  KAppTreeListItem * newItem;
193  for( KServiceGroup::List::ConstIterator it = list.begin();
194  it != list.end(); it++)
195  {
196  TQString icon;
197  TQString text;
198  TQString relPath;
199  TQString exec;
200  bool isDir = false;
201  KSycocaEntry *p = (*it);
202  if (p->isType(KST_KService))
203  {
204  KService *service = static_cast<KService *>(p);
205 
206  if (service->noDisplay())
207  continue;
208 
209  icon = service->icon();
210  text = service->name();
211  exec = service->exec();
212  }
213  else if (p->isType(KST_KServiceGroup))
214  {
215  KServiceGroup *serviceGroup = static_cast<KServiceGroup *>(p);
216 
217  if (serviceGroup->noDisplay() || serviceGroup->childCount() == 0)
218  continue;
219 
220  icon = serviceGroup->icon();
221  text = serviceGroup->caption();
222  relPath = serviceGroup->relPath();
223  isDir = true;
224  }
225  else
226  {
227  kdWarning(250) << "KServiceGroup: Unexpected object in list!" << endl;
228  continue;
229  }
230 
231  TQPixmap pixmap = appIcon( icon );
232 
233  if (item)
234  newItem = new KAppTreeListItem( item, text, pixmap, false, isDir,
235  relPath, exec );
236  else
237  newItem = new KAppTreeListItem( this, text, pixmap, false, isDir,
238  relPath, exec );
239  if (isDir)
240  newItem->setExpandable( true );
241  }
242 }
243 
244 
245 // ----------------------------------------------------------------------
246 
247 void TDEApplicationTree::slotItemHighlighted(TQListViewItem* i)
248 {
249  // i may be 0 (see documentation)
250  if(!i)
251  return;
252 
253  KAppTreeListItem *item = (KAppTreeListItem *) i;
254 
255  currentitem = item;
256 
257  if( (!item->directory ) && (!item->exec.isEmpty()) )
258  emit highlighted( item->text(0), item->exec );
259 }
260 
261 
262 // ----------------------------------------------------------------------
263 
264 void TDEApplicationTree::slotSelectionChanged(TQListViewItem* i)
265 {
266  // i may be 0 (see documentation)
267  if(!i)
268  return;
269 
270  KAppTreeListItem *item = (KAppTreeListItem *) i;
271 
272  currentitem = item;
273 
274  if( ( !item->directory ) && (!item->exec.isEmpty() ) )
275  emit selected( item->text(0), item->exec );
276 }
277 
278 // ----------------------------------------------------------------------
279 
280 void TDEApplicationTree::resizeEvent( TQResizeEvent * e)
281 {
282  setColumnWidth(0, width()-TQApplication::style().pixelMetric(TQStyle::PM_ScrollBarExtent)
283  -2*TQApplication::style().pixelMetric(TQStyle::PM_DefaultFrameWidth));
284  TDEListView::resizeEvent(e);
285 }
286 
287 // Prune empty directories from the tree
288 void TDEApplicationTree::cleanupTree()
289 {
290  TQListViewItem *item=firstChild();
291  while(item!=0)
292  {
293  if(item->isExpandable())
294  {
295  TQListViewItem *temp=item->itemBelow();
296  if(item->text(0)!=i18n("Applications"))
297  item->setOpen(false);
298  item=temp;
299  continue;
300  }
301  item=item->itemBelow();
302  }
303 }
304 
305 /***************************************************************
306  *
307  * KOpenWithDlg
308  *
309  ***************************************************************/
310 class KOpenWithDlgPrivate
311 {
312 public:
313  KOpenWithDlgPrivate() : saveNewApps(false) { };
314  TQPushButton* ok;
315  bool saveNewApps;
316  KService::Ptr curService;
317 };
318 
319 KOpenWithDlg::KOpenWithDlg( const KURL::List& _urls, TQWidget* parent )
320  :TQDialog( parent, "openwith", true )
321 {
322  setCaption( i18n( "Open With" ) );
323  TQString text;
324  if( _urls.count() == 1 )
325  {
326  text = i18n("<qt>Select the program that should be used to open <b>%1</b>. "
327  "If the program is not listed, enter the name or click "
328  "the browse button.</qt>").arg( _urls.first().fileName() );
329  }
330  else
331  // Should never happen ??
332  text = i18n( "Choose the name of the program with which to open the selected files." );
333  setServiceType( _urls );
334  init( text, TQString() );
335 }
336 
337 KOpenWithDlg::KOpenWithDlg( const KURL::List& _urls, const TQString&_text,
338  const TQString& _value, TQWidget *parent)
339  :TQDialog( parent, "openwith", true )
340 {
341  TQString caption = KStringHandler::csqueeze( _urls.first().prettyURL() );
342  if (_urls.count() > 1)
343  caption += TQString::fromLatin1("...");
344  setCaption(caption);
345  setServiceType( _urls );
346  init( _text, _value );
347 }
348 
349 KOpenWithDlg::KOpenWithDlg( const TQString &serviceType, const TQString& value,
350  TQWidget *parent)
351  :TQDialog( parent, "openwith", true )
352 {
353  setCaption(i18n("Choose Application for %1").arg(serviceType));
354  TQString text = i18n("<qt>Select the program for the file type: <b>%1</b>. "
355  "If the program is not listed, enter the name or click "
356  "the browse button.</qt>").arg(serviceType);
357  qServiceType = serviceType;
358  init( text, value );
359  if (remember)
360  remember->hide();
361 }
362 
363 KOpenWithDlg::KOpenWithDlg( TQWidget *parent)
364  :TQDialog( parent, "openwith", true )
365 {
366  setCaption(i18n("Choose Application"));
367  TQString text = i18n("<qt>Select a program. "
368  "If the program is not listed, enter the name or click "
369  "the browse button.</qt>");
370  qServiceType = TQString::null;
371  init( text, TQString::null );
372 }
373 
374 void KOpenWithDlg::setServiceType( const KURL::List& _urls )
375 {
376  if ( _urls.count() == 1 )
377  {
378  qServiceType = KMimeType::findByURL( _urls.first())->name();
379  if (qServiceType == TQString::fromLatin1("application/octet-stream"))
380  qServiceType = TQString::null;
381  }
382  else
383  qServiceType = TQString::null;
384 }
385 
386 void KOpenWithDlg::init( const TQString& _text, const TQString& _value )
387 {
388  d = new KOpenWithDlgPrivate;
389  bool bReadOnly = kapp && !kapp->authorize("shell_access");
390  m_terminaldirty = false;
391  m_pTree = 0L;
392  m_pService = 0L;
393  d->curService = 0L;
394 
395  TQBoxLayout *topLayout = new TQVBoxLayout( this, KDialog::marginHint(),
396  KDialog::spacingHint() );
397  label = new TQLabel( _text, this );
398  topLayout->addWidget(label);
399 
400  TQHBoxLayout* hbox = new TQHBoxLayout(topLayout);
401 
402  TQToolButton *clearButton = new TQToolButton( this );
403  clearButton->setIconSet( BarIcon( "locationbar_erase" ) );
404  clearButton->setFixedSize( clearButton->sizeHint() );
405  connect( clearButton, TQT_SIGNAL( clicked() ), TQT_SLOT( slotClear() ) );
406  TQToolTip::add( clearButton, i18n( "Clear input field" ) );
407 
408  hbox->addWidget( clearButton );
409 
410  if (!bReadOnly)
411  {
412  // init the history combo and insert it into the URL-Requester
413  KHistoryCombo *combo = new KHistoryCombo();
414  combo->setDuplicatesEnabled( false );
415  TDEConfig *kc = TDEGlobal::config();
416  TDEConfigGroupSaver ks( kc, TQString::fromLatin1("Open-with settings") );
417  int max = kc->readNumEntry( TQString::fromLatin1("Maximum history"), 15 );
418  combo->setMaxCount( max );
419  int mode = kc->readNumEntry(TQString::fromLatin1("CompletionMode"),
420  TDEGlobalSettings::completionMode());
421  combo->setCompletionMode((TDEGlobalSettings::Completion)mode);
422  TQStringList list = kc->readListEntry( TQString::fromLatin1("History") );
423  combo->setHistoryItems( list, true );
424  edit = new KURLRequester( combo, this );
425  }
426  else
427  {
428  clearButton->hide();
429  edit = new KURLRequester( this );
430  edit->lineEdit()->setReadOnly(true);
431  edit->button()->hide();
432  }
433 
434  edit->setURL( _value );
435  TQWhatsThis::add(edit,i18n(
436  "Following the command, you can have several place holders which will be replaced "
437  "with the actual values when the actual program is run:\n"
438  "%f - a single file name\n"
439  "%F - a list of files; use for applications that can open several local files at once\n"
440  "%u - a single URL\n"
441  "%U - a list of URLs\n"
442  "%d - the directory of the file to open\n"
443  "%D - a list of directories\n"
444  "%i - the icon\n"
445  "%m - the mini-icon\n"
446  "%c - the comment"));
447 
448  hbox->addWidget(edit);
449 
450  if ( edit->comboBox() ) {
451  KURLCompletion *comp = new KURLCompletion( KURLCompletion::ExeCompletion );
452  edit->comboBox()->setCompletionObject( comp );
453  edit->comboBox()->setAutoDeleteCompletionObject( true );
454  }
455 
456  connect ( edit, TQT_SIGNAL(returnPressed()), TQT_SLOT(slotOK()) );
457  connect ( edit, TQT_SIGNAL(textChanged(const TQString&)), TQT_SLOT(slotTextChanged()) );
458 
459  m_pTree = new TDEApplicationTree( this );
460  topLayout->addWidget(m_pTree);
461 
462  connect( m_pTree, TQT_SIGNAL( selected( const TQString&, const TQString& ) ),
463  TQT_SLOT( slotSelected( const TQString&, const TQString& ) ) );
464  connect( m_pTree, TQT_SIGNAL( highlighted( const TQString&, const TQString& ) ),
465  TQT_SLOT( slotHighlighted( const TQString&, const TQString& ) ) );
466  connect( m_pTree, TQT_SIGNAL( doubleClicked(TQListViewItem*) ),
467  TQT_SLOT( slotDbClick() ) );
468 
469  terminal = new TQCheckBox( i18n("Run in &terminal"), this );
470  if (bReadOnly)
471  terminal->hide();
472  connect(terminal, TQT_SIGNAL(toggled(bool)), TQT_SLOT(slotTerminalToggled(bool)));
473 
474  topLayout->addWidget(terminal);
475 
476  TQBoxLayout* nocloseonexitLayout = new TQHBoxLayout( 0, 0, KDialog::spacingHint() );
477  TQSpacerItem* spacer = new TQSpacerItem( 20, 0, TQSizePolicy::Fixed, TQSizePolicy::Minimum );
478  nocloseonexitLayout->addItem( spacer );
479 
480  nocloseonexit = new TQCheckBox( i18n("&Do not close when command exits"), this );
481  nocloseonexit->setChecked( false );
482  nocloseonexit->setDisabled( true );
483 
484  // check to see if we use konsole if not disable the nocloseonexit
485  // because we don't know how to do this on other terminal applications
486  TDEConfigGroup confGroup( TDEGlobal::config(), TQString::fromLatin1("General") );
487  TQString preferredTerminal = confGroup.readPathEntry("TerminalApplication", TQString::fromLatin1("konsole"));
488 
489  if (bReadOnly || preferredTerminal != "konsole")
490  nocloseonexit->hide();
491 
492  nocloseonexitLayout->addWidget( nocloseonexit );
493  topLayout->addLayout( nocloseonexitLayout );
494 
495  if (!qServiceType.isNull())
496  {
497  remember = new TQCheckBox(i18n("&Remember application association for this type of file"), this);
498  // remember->setChecked(true);
499  topLayout->addWidget(remember);
500  }
501  else
502  remember = 0L;
503 
504  // Use KButtonBox for the aligning pushbuttons nicely
505  KButtonBox* b = new KButtonBox( this );
506  b->addStretch( 2 );
507 
508  d->ok = b->addButton( KStdGuiItem::ok() );
509  d->ok->setDefault( true );
510  connect( d->ok, TQT_SIGNAL( clicked() ), TQT_SLOT( slotOK() ) );
511 
512  TQPushButton* cancel = b->addButton( KStdGuiItem::cancel() );
513  connect( cancel, TQT_SIGNAL( clicked() ), TQT_SLOT( reject() ) );
514 
515  b->layout();
516  topLayout->addWidget( b );
517 
518  //edit->setText( _value );
519  // This is what caused "can't click on items before clicking on Name header".
520  // Probably due to the resizeEvent handler using width().
521  //resize( minimumWidth(), sizeHint().height() );
522  edit->setFocus();
523  slotTextChanged();
524 }
525 
526 
527 // ----------------------------------------------------------------------
528 
529 KOpenWithDlg::~KOpenWithDlg()
530 {
531  delete d;
532  d = 0;
533 }
534 
535 // ----------------------------------------------------------------------
536 
537 void KOpenWithDlg::slotClear()
538 {
539  edit->setURL(TQString::null);
540  edit->setFocus();
541 }
542 
543 
544 // ----------------------------------------------------------------------
545 
546 void KOpenWithDlg::slotSelected( const TQString& /*_name*/, const TQString& _exec )
547 {
548  kdDebug(250)<<"KOpenWithDlg::slotSelected"<<endl;
549  KService::Ptr pService = d->curService;
550  edit->setURL( _exec ); // calls slotTextChanged :(
551  d->curService = pService;
552 }
553 
554 
555 // ----------------------------------------------------------------------
556 
557 void KOpenWithDlg::slotHighlighted( const TQString& _name, const TQString& )
558 {
559  kdDebug(250)<<"KOpenWithDlg::slotHighlighted"<<endl;
560  qName = _name;
561  d->curService = KService::serviceByName( qName );
562  if (!m_terminaldirty)
563  {
564  // ### indicate that default value was restored
565  terminal->setChecked(d->curService->terminal());
566  TQString terminalOptions = d->curService->terminalOptions();
567  nocloseonexit->setChecked( (terminalOptions.contains( "--noclose" ) > 0) );
568  m_terminaldirty = false; // slotTerminalToggled changed it
569  }
570 }
571 
572 // ----------------------------------------------------------------------
573 
574 void KOpenWithDlg::slotTextChanged()
575 {
576  kdDebug(250)<<"KOpenWithDlg::slotTextChanged"<<endl;
577  // Forget about the service
578  d->curService = 0L;
579  d->ok->setEnabled( !edit->url().isEmpty());
580 }
581 
582 // ----------------------------------------------------------------------
583 
584 void KOpenWithDlg::slotTerminalToggled(bool)
585 {
586  // ### indicate that default value was overridden
587  m_terminaldirty = true;
588  nocloseonexit->setDisabled( ! terminal->isChecked() );
589 }
590 
591 // ----------------------------------------------------------------------
592 
593 void KOpenWithDlg::slotDbClick()
594 {
595  if (m_pTree->isDirSel() ) return; // check if a directory is selected
596  slotOK();
597 }
598 
599 void KOpenWithDlg::setSaveNewApplications(bool b)
600 {
601  d->saveNewApps = b;
602 }
603 
604 void KOpenWithDlg::slotOK()
605 {
606  TQString typedExec(edit->url());
607  TQString fullExec(typedExec);
608 
609  TQString serviceName;
610  TQString initialServiceName;
611  TQString preferredTerminal;
612  m_pService = d->curService;
613  if (!m_pService) {
614  // No service selected - check the command line
615 
616  // Find out the name of the service from the command line, removing args and paths
617  serviceName = KRun::binaryName( typedExec, true );
618  if (serviceName.isEmpty())
619  {
620  // TODO add a KMessageBox::error here after the end of the message freeze
621  return;
622  }
623  initialServiceName = serviceName;
624  kdDebug(250) << "initialServiceName=" << initialServiceName << endl;
625  int i = 1; // We have app, app-2, app-3... Looks better for the user.
626  bool ok = false;
627  // Check if there's already a service by that name, with the same Exec line
628  do {
629  kdDebug(250) << "looking for service " << serviceName << endl;
630  KService::Ptr serv = KService::serviceByDesktopName( serviceName );
631  ok = !serv; // ok if no such service yet
632  // also ok if we find the exact same service (well, "kwrite" == "kwrite %U"
633  if ( serv && serv->type() == "Application")
634  {
635  TQString exec = serv->exec();
636  fullExec = exec;
637  exec.replace("%u", "", false);
638  exec.replace("%f", "", false);
639  exec.replace("-caption %c", "");
640  exec.replace("-caption \"%c\"", "");
641  exec.replace("%i", "");
642  exec.replace("%m", "");
643  exec = exec.simplifyWhiteSpace();
644  if (exec == typedExec)
645  {
646  ok = true;
647  m_pService = serv;
648  kdDebug(250) << k_funcinfo << "OK, found identical service: " << serv->desktopEntryPath() << endl;
649  }
650  }
651  if (!ok) // service was found, but it was different -> keep looking
652  {
653  ++i;
654  serviceName = initialServiceName + "-" + TQString::number(i);
655  }
656  }
657  while (!ok);
658  }
659  if ( m_pService )
660  {
661  // Existing service selected
662  serviceName = m_pService->name();
663  initialServiceName = serviceName;
664  fullExec = m_pService->exec();
665  }
666 
667  if (terminal->isChecked())
668  {
669  TDEConfigGroup confGroup( TDEGlobal::config(), TQString::fromLatin1("General") );
670  preferredTerminal = confGroup.readPathEntry("TerminalApplication", TQString::fromLatin1("konsole"));
671  m_command = preferredTerminal;
672  // only add --noclose when we are sure it is konsole we're using
673  if (preferredTerminal == "konsole" && nocloseonexit->isChecked())
674  m_command += TQString::fromLatin1(" --noclose");
675  m_command += TQString::fromLatin1(" -e ");
676  m_command += edit->url();
677  kdDebug(250) << "Setting m_command to " << m_command << endl;
678  }
679  if ( m_pService && terminal->isChecked() != m_pService->terminal() )
680  m_pService = 0L; // It's not exactly this service we're running
681 
682  bool bRemember = remember && remember->isChecked();
683 
684  if ( !bRemember && m_pService)
685  {
686  accept();
687  return;
688  }
689 
690  if (!bRemember && !d->saveNewApps)
691  {
692  // Create temp service
693  m_pService = new KService(initialServiceName, fullExec, TQString::null);
694  if (terminal->isChecked())
695  {
696  m_pService->setTerminal(true);
697  // only add --noclose when we are sure it is konsole we're using
698  if (preferredTerminal == "konsole" && nocloseonexit->isChecked())
699  m_pService->setTerminalOptions("--noclose");
700  }
701  accept();
702  return;
703  }
704 
705  // if we got here, we can't seem to find a service for what they
706  // wanted. The other possibility is that they have asked for the
707  // association to be remembered. Create/update service.
708 
709  TQString newPath;
710  TQString oldPath;
711  TQString menuId;
712  if (m_pService)
713  {
714  oldPath = m_pService->desktopEntryPath();
715  newPath = m_pService->locateLocal();
716  menuId = m_pService->menuId();
717  kdDebug(250) << "Updating exitsing service " << m_pService->desktopEntryPath() << " ( " << newPath << " ) " << endl;
718  }
719  else
720  {
721  newPath = KService::newServicePath(false /* hidden */, serviceName, &menuId);
722  kdDebug(250) << "Creating new service " << serviceName << " ( " << newPath << " ) " << endl;
723  }
724 
725  int maxPreference = 1;
726  if (!qServiceType.isEmpty())
727  {
728  KServiceTypeProfile::OfferList offerList = KServiceTypeProfile::offers( qServiceType );
729  if (!offerList.isEmpty())
730  maxPreference = offerList.first().preference();
731  }
732 
733  KDesktopFile *desktop = 0;
734  if (!oldPath.isEmpty() && (oldPath != newPath))
735  {
736  KDesktopFile orig(oldPath, true);
737  desktop = orig.copyTo(newPath);
738  }
739  else
740  {
741  desktop = new KDesktopFile(newPath);
742  }
743  desktop->writeEntry("Type", TQString::fromLatin1("Application"));
744  desktop->writeEntry("Name", initialServiceName);
745  desktop->writePathEntry("Exec", fullExec);
746  if (terminal->isChecked())
747  {
748  desktop->writeEntry("Terminal", true);
749  // only add --noclose when we are sure it is konsole we're using
750  if (preferredTerminal == "konsole" && nocloseonexit->isChecked())
751  desktop->writeEntry("TerminalOptions", "--noclose");
752  }
753  else
754  {
755  desktop->writeEntry("Terminal", false);
756  }
757  desktop->writeEntry("X-TDE-InitialPreference", maxPreference + 1);
758 
759 
760  if (bRemember || d->saveNewApps)
761  {
762  TQStringList mimeList = desktop->readListEntry("MimeType", ';');
763  if (!qServiceType.isEmpty() && !mimeList.contains(qServiceType))
764  mimeList.append(qServiceType);
765  desktop->writeEntry("MimeType", mimeList, ';');
766 
767  if ( !qServiceType.isEmpty() )
768  {
769  // Also make sure the "auto embed" setting for this mimetype is off
770  KDesktopFile mimeDesktop( locateLocal( "mime", qServiceType + ".desktop" ) );
771  mimeDesktop.writeEntry( "X-TDE-AutoEmbed", false );
772  mimeDesktop.sync();
773  }
774  }
775 
776  // write it all out to the file
777  desktop->sync();
778  delete desktop;
779 
780  KService::rebuildKSycoca(this);
781 
782  m_pService = KService::serviceByMenuId( menuId );
783 
784  Q_ASSERT( m_pService );
785 
786  accept();
787 }
788 
789 TQString KOpenWithDlg::text() const
790 {
791  if (!m_command.isEmpty())
792  return m_command;
793  else
794  return edit->url();
795 }
796 
797 void KOpenWithDlg::hideNoCloseOnExit()
798 {
799  // uncheck the checkbox because the value could be used when "Run in Terminal" is selected
800  nocloseonexit->setChecked( false );
801  nocloseonexit->hide();
802 }
803 
804 void KOpenWithDlg::hideRunInTerminal()
805 {
806  terminal->hide();
807  hideNoCloseOnExit();
808 }
809 
810 void KOpenWithDlg::accept()
811 {
812  KHistoryCombo *combo = static_cast<KHistoryCombo*>( edit->comboBox() );
813  if ( combo ) {
814  combo->addToHistory( edit->url() );
815 
816  TDEConfig *kc = TDEGlobal::config();
817  TDEConfigGroupSaver ks( kc, TQString::fromLatin1("Open-with settings") );
818  kc->writeEntry( TQString::fromLatin1("History"), combo->historyItems() );
819  kc->writeEntry(TQString::fromLatin1("CompletionMode"),
820  combo->completionMode());
821  // don't store the completion-list, as it contains all of KURLCompletion's
822  // executables
823  kc->sync();
824  }
825 
826  TQDialog::accept();
827 }
828 
829 
831 
832 #ifndef KDE_NO_COMPAT
833 bool KFileOpenWithHandler::displayOpenWithDialog( const KURL::List& urls )
834 {
835  KOpenWithDlg l( urls, i18n("Open with:"), TQString::null, 0L );
836  if ( l.exec() )
837  {
838  KService::Ptr service = l.service();
839  if ( !!service )
840  return KRun::run( *service, urls );
841 
842  kdDebug(250) << "No service set, running " << l.text() << endl;
843  return KRun::run( l.text(), urls );
844  }
845  return false;
846 }
847 #endif
848 
849 #include "kopenwith.moc"
850 #include "kopenwith_p.moc"
851 
KURLRequester::url
TQString url() const
Definition: kurlrequester.cpp:268
KOpenWithDlg::setServiceType
void setServiceType(const KURL::List &_urls)
Determine service type from URLs.
Definition: kopenwith.cpp:374
KOpenWithDlg::~KOpenWithDlg
~KOpenWithDlg()
Destructor.
Definition: kopenwith.cpp:529
KURLRequester::lineEdit
KLineEdit * lineEdit() const
Definition: kurlrequester.cpp:369
KOpenWithDlg::slotClear
void slotClear()
The slot for clearing the edit widget.
Definition: kopenwith.cpp:537
KOpenWithDlg
"Open with" dialog box.
Definition: kopenwith.h:46
KOpenWithDlg::accept
virtual void accept()
Reimplemented from TQDialog::accept() to save history of the combobox.
Definition: kopenwith.cpp:810
KURLRequester::comboBox
KComboBox * comboBox() const
Definition: kurlrequester.cpp:374
KOpenWithDlg::hideNoCloseOnExit
void hideNoCloseOnExit()
Hide the "Do not &close when command exits" Checkbox.
Definition: kopenwith.cpp:797
KURLRequester::setURL
void setURL(const TQString &url)
Sets the url in the lineedit to url.
Definition: kurlrequester.cpp:235
KOpenWithDlg::hideRunInTerminal
void hideRunInTerminal()
Hide the "Run in &terminal" Checkbox.
Definition: kopenwith.cpp:804
KURLRequester
This class is a widget showing a lineedit and a button, which invokes a filedialog.
Definition: kurlrequester.h:56
KOpenWithDlg::setSaveNewApplications
void setSaveNewApplications(bool b)
Set whether a new .desktop file should be created if the user selects an application for which no cor...
Definition: kopenwith.cpp:599
KOpenWithDlg::KOpenWithDlg
KOpenWithDlg(const KURL::List &urls, TQWidget *parent=0L)
Create a dialog that asks for a application to open a given URL(s) with.
Definition: kopenwith.cpp:319
KURLRequester::button
KPushButton * button() const
Definition: kurlrequester.cpp:398
KOpenWithDlg::text
TQString text() const
Definition: kopenwith.cpp:789
KOpenWithDlg::init
void init(const TQString &text, const TQString &value)
Create a dialog that asks for a application to open a given URL(s) with.
Definition: kopenwith.cpp:386
KFileOpenWithHandler::displayOpenWithDialog
virtual bool displayOpenWithDialog(const KURL::List &urls)
Opens an open-with dialog box for urls.
Definition: kopenwith.cpp:833
KOpenWithDlg::service
KService::Ptr service() const
Definition: kopenwith.h:114

tdeio/tdefile

Skip menu "tdeio/tdefile"
  • Main Page
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Class Members
  • Related Pages

tdeio/tdefile

Skip menu "tdeio/tdefile"
  • arts
  • dcop
  • dnssd
  • interfaces
  •   kspeech
  •     interface
  •     library
  •   tdetexteditor
  • kate
  • kded
  • kdoctools
  • kimgio
  • kjs
  • libtdemid
  • libtdescreensaver
  • tdeabc
  • tdecmshell
  • tdecore
  • tdefx
  • tdehtml
  • tdeinit
  • tdeio
  •   bookmarks
  •   httpfilter
  •   kpasswdserver
  •   kssl
  •   tdefile
  •   tdeio
  •   tdeioexec
  • tdeioslave
  •   http
  • tdemdi
  •   tdemdi
  • tdenewstuff
  • tdeparts
  • tdeprint
  • tderandr
  • tderesources
  • tdespell2
  • tdesu
  • tdeui
  • tdeunittest
  • tdeutils
  • tdewallet
Generated for tdeio/tdefile by doxygen 1.8.11
This website is maintained by Timothy Pearson.