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

tdeio/tdeio

  • tdeio
  • tdeio
kmimetypechooser.cpp
1 /* This file is part of the KDE libraries
2  Copyright (C) 2001 - 2004 Anders Lund <anders@alweb.dk>
3 
4  This library is free software; you can redistribute it and/or
5  modify it under the terms of the GNU Library General Public
6  License version 2 as published by the Free Software Foundation.
7 
8  This library is distributed in the hope that it will be useful,
9  but WITHOUT ANY WARRANTY; without even the implied warranty of
10  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11  Library General Public License for more details.
12 
13  You should have received a copy of the GNU Library General Public License
14  along with this library; see the file COPYING.LIB. If not, write to
15  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
16  Boston, MA 02110-1301, USA.
17 */
18 
19 #include "kmimetypechooser.h"
20 
21 #include <tdeconfig.h>
22 #include <kiconloader.h>
23 #include <tdelistview.h>
24 #include <tdelocale.h>
25 #include <kmimetype.h>
26 #include <kprocess.h>
27 #include <krun.h>
28 #include <tdesycoca.h>
29 
30 #include <tqlabel.h>
31 #include <tqlayout.h>
32 #include <tqlineedit.h>
33 #include <tqpushbutton.h>
34 #include <tqwhatsthis.h>
35 
36 //BEGIN KMimeTypeChooserPrivate
37 class KMimeTypeChooserPrivate
38 {
39  public:
40  TDEListView *lvMimeTypes;
41  TQPushButton *btnEditMimeType;
42 
43  TQString defaultgroup;
44  TQStringList groups;
45  int visuals;
46 };
47 //END
48 
49 //BEGIN KMimeTypeChooser
50 KMimeTypeChooser::KMimeTypeChooser( const TQString &text,
51  const TQStringList &selMimeTypes,
52  const TQString &defaultGroup,
53  const TQStringList &groupsToShow,
54  int visuals,
55  TQWidget *parent,
56  const char *name )
57  : TQVBox( parent, name )
58 {
59  d = new KMimeTypeChooserPrivate();
60  d->lvMimeTypes = 0;
61  d->btnEditMimeType = 0;
62  d->defaultgroup = defaultGroup;
63  d->groups = groupsToShow;
64  d->visuals = visuals;
65 
66  setSpacing( KDialogBase::spacingHint() );
67 
68  if ( !text.isEmpty() )
69  {
70  new TQLabel( text, this );
71  }
72 
73  d->lvMimeTypes = new TDEListView( this );
74 
75  d->lvMimeTypes->addColumn( i18n("Mime Type") );
76 // d->lvMimeTypes->setColumnWidthMode( 0, TQListView::Manual );
77 
78  if ( visuals & Comments )
79  {
80  d->lvMimeTypes->addColumn( i18n("Comment") );
81  d->lvMimeTypes->setColumnWidthMode( 1, TQListView::Manual );
82  }
83  if ( visuals & Patterns )
84  d->lvMimeTypes->addColumn( i18n("Patterns") );
85 
86  d->lvMimeTypes->setRootIsDecorated( true );
87 
88  loadMimeTypes( selMimeTypes );
89 
90  if (visuals & KMimeTypeChooser::EditButton)
91  {
92  TQHBox *btns = new TQHBox( this );
93  ((TQBoxLayout*)btns->layout())->addStretch(1);
94  d->btnEditMimeType = new TQPushButton( i18n("&Edit..."), btns );
95 
96  connect( d->btnEditMimeType, TQT_SIGNAL(clicked()), this, TQT_SLOT(editMimeType()) );
97  d->btnEditMimeType->setEnabled( false );
98  connect( d->lvMimeTypes, TQT_SIGNAL( doubleClicked ( TQListViewItem * )),
99  this, TQT_SLOT( editMimeType()));
100  connect( d->lvMimeTypes, TQT_SIGNAL(currentChanged(TQListViewItem*)),
101  this, TQT_SLOT(slotCurrentChanged(TQListViewItem*)) );
102 
103  TQWhatsThis::add( d->btnEditMimeType, i18n(
104  "Click this button to display the familiar TDE mime type editor.") );
105  }
106 }
107 
108 KMimeTypeChooser::~KMimeTypeChooser()
109 {
110  delete d;
111 }
112 
113 void KMimeTypeChooser::loadMimeTypes( const TQStringList &_selectedMimeTypes )
114 {
115  TQStringList selMimeTypes;
116 
117  if ( !_selectedMimeTypes.isEmpty() )
118  selMimeTypes = _selectedMimeTypes;
119  else
120  selMimeTypes = mimeTypes();
121 
122  d->lvMimeTypes->clear();
123 
124  TQMap<TQString,TQListViewItem*> groups;
125  // thanks to tdebase/kcontrol/filetypes/filetypesview
126  KMimeType::List mimetypes = KMimeType::allMimeTypes();
127  TQValueListIterator<KMimeType::Ptr> it(mimetypes.begin());
128 
129  TQListViewItem *groupItem;
130  bool agroupisopen = false;
131  TQListViewItem *idefault = 0; //open this, if all other fails
132  TQListViewItem *firstChecked = 0; // make this one visible after the loop
133 
134  for (; it != mimetypes.end(); ++it)
135  {
136  TQString mimetype = (*it)->name();
137  int index = mimetype.find("/");
138  TQString maj = mimetype.left(index);
139 
140  if ( d->groups.count() && !d->groups.contains( maj ) )
141  continue;
142 
143  TQString min = mimetype.right(mimetype.length() - (index+1));
144 
145  TQMapIterator<TQString,TQListViewItem*> mit = groups.find( maj );
146  if ( mit == groups.end() )
147  {
148  groupItem = new TQListViewItem( d->lvMimeTypes, maj );
149  groups.insert( maj, groupItem );
150  if ( maj == d->defaultgroup )
151  idefault = groupItem;
152  }
153  else
154  groupItem = mit.data();
155 
156  TQCheckListItem *item = new TQCheckListItem( groupItem, min, TQCheckListItem::CheckBox );
157  item->setPixmap( 0, SmallIcon( (*it)->icon(TQString::null,false) ) );
158 
159  int cl = 1;
160 
161  if ( d->visuals & Comments )
162  {
163  item->setText( cl, (*it)->comment(TQString::null, false) );
164  cl++;
165  }
166 
167  if ( d->visuals & Patterns )
168  item->setText( cl, (*it)->patterns().join("; ") );
169 
170  if ( selMimeTypes.contains(mimetype) )
171  {
172  item->setOn( true );
173  groupItem->setOpen( true );
174  agroupisopen = true;
175  if ( !firstChecked )
176  firstChecked = item;
177  }
178  }
179 
180  if ( firstChecked )
181  d->lvMimeTypes->ensureItemVisible( firstChecked );
182 
183  if ( !agroupisopen && idefault )
184  {
185  idefault->setOpen( true );
186  d->lvMimeTypes->ensureItemVisible( idefault );
187  }
188 }
189 
190 void KMimeTypeChooser::editMimeType()
191 {
192  if ( !(d->lvMimeTypes->currentItem() && (d->lvMimeTypes->currentItem())->parent()) )
193  return;
194  TQString mt = (d->lvMimeTypes->currentItem()->parent())->text( 0 ) + "/" + (d->lvMimeTypes->currentItem())->text( 0 );
195  // thanks to libkonq/konq_operations.cpp
196  connect( KSycoca::self(), TQT_SIGNAL(databaseChanged()),
197  this, TQT_SLOT(slotSycocaDatabaseChanged()) );
198  TQString keditfiletype = TQString::fromLatin1("keditfiletype");
199  KRun::runCommand( keditfiletype
200  + " --parent " + TQString::number( (ulong)topLevelWidget()->winId())
201  + " " + TDEProcess::quote(mt),
202  keditfiletype, keditfiletype /*unused*/);
203 }
204 
205 void KMimeTypeChooser::slotCurrentChanged(TQListViewItem* i)
206 {
207  if ( d->btnEditMimeType )
208  d->btnEditMimeType->setEnabled( i->parent() );
209 }
210 
211 void KMimeTypeChooser::slotSycocaDatabaseChanged()
212 {
213  if ( KSycoca::self()->isChanged("mime") )
214  loadMimeTypes();
215 }
216 
217 TQStringList KMimeTypeChooser::mimeTypes() const
218 {
219  TQStringList l;
220  TQListViewItemIterator it( d->lvMimeTypes );
221  for (; it.current(); ++it)
222  {
223  if ( it.current()->parent() && ((TQCheckListItem*)it.current())->isOn() )
224  l << it.current()->parent()->text(0) + "/" + it.current()->text(0); // FIXME uncecked, should be Ok unless someone changes mimetypes during this!
225  }
226  return l;
227 }
228 
229 TQStringList KMimeTypeChooser::patterns() const
230 {
231  TQStringList l;
232  KMimeType::Ptr p;
233  TQString defMT = KMimeType::defaultMimeType();
234  TQListViewItemIterator it( d->lvMimeTypes );
235  for (; it.current(); ++it)
236  {
237  if ( it.current()->parent() && ((TQCheckListItem*)it.current())->isOn() )
238  {
239  p = KMimeType::mimeType( it.current()->parent()->text(0) + "/" + it.current()->text(0) );
240  if ( p->name() != defMT )
241  l += p->patterns();
242  }
243  }
244  return l;
245 }
246 //END
247 
248 //BEGIN KMimeTypeChooserDialog
249 KMimeTypeChooserDialog::KMimeTypeChooserDialog(
250  const TQString &caption,
251  const TQString& text,
252  const TQStringList &selMimeTypes,
253  const TQString &defaultGroup,
254  const TQStringList &groupsToShow,
255  int visuals,
256  TQWidget *parent, const char *name )
257  : KDialogBase(parent, name, true, caption, Cancel|Ok, Ok)
258 {
259  m_chooser = new KMimeTypeChooser( text, selMimeTypes,
260  defaultGroup, groupsToShow, visuals,
261  this, "chooser" );
262  setMainWidget(m_chooser);
263 
264  TDEConfigGroup group( TDEGlobal::config(), "KMimeTypeChooserDialog");
265  TQSize defaultSize( 400, 300 );
266  resize( group.readSizeEntry("size", &defaultSize) );
267 }
268 
269 KMimeTypeChooserDialog::KMimeTypeChooserDialog(
270  const TQString &caption,
271  const TQString& text,
272  const TQStringList &selMimeTypes,
273  const TQString &defaultGroup,
274  TQWidget *parent, const char *name )
275  : KDialogBase(parent, name, true, caption, Cancel|Ok, Ok)
276 {
277  m_chooser = new KMimeTypeChooser( text, selMimeTypes,
278  defaultGroup, TQStringList(),
279  KMimeTypeChooser::Comments|KMimeTypeChooser::Patterns|KMimeTypeChooser::EditButton,
280  this, "chooser" );
281  setMainWidget(m_chooser);
282 
283  TDEConfigGroup group( TDEGlobal::config(), "KMimeTypeChooserDialog");
284  TQSize defaultSize( 400, 300 );
285  resize( group.readSizeEntry("size", &defaultSize) );
286 }
287 
288 
289 KMimeTypeChooserDialog::~KMimeTypeChooserDialog()
290 {
291  TDEConfigGroup group( TDEGlobal::config(), "KMimeTypeChooserDialog");
292  group.writeEntry("size", size());
293 }
294 
295 //END KMimeTypeChooserDialog
296 #include "kmimetypechooser.moc"
KMimeTypeChooser::Comments
Show the Mimetypes Comment field in a column ("HTML Document").
Definition: kmimetypechooser.h:43
KMimeType::mimeType
static Ptr mimeType(const TQString &_name)
Retrieve a pointer to the mime type _name or a pointer to the default mime type "application/octet-st...
Definition: kmimetype.cpp:141
KMimeTypeChooser::Patterns
Show the Mimetypes Patterns field in a column ("*.html;*.htm").
Definition: kmimetypechooser.h:44
KMimeTypeChooser::KMimeTypeChooser
KMimeTypeChooser(const TQString &text=TQString::null, const TQStringList &selectedMimeTypes=0, const TQString &defaultGroup=TQString::null, const TQStringList &groupsToShow=TQStringList(), int visuals=Comments|Patterns|EditButton, TQWidget *parent=0, const char *name=0)
Create a new KMimeTypeChooser.
Definition: kmimetypechooser.cpp:50
KMimeType::defaultMimeType
static const TQString & defaultMimeType()
Returns the name of the default mimetype.
Definition: kmimetype.cpp:1160
TDEIO::mimetype
TDEIO_EXPORT MimetypeJob * mimetype(const KURL &url, bool showProgressInfo=true)
Find mimetype for one file or directory.
Definition: job.cpp:1544
KMimeTypeChooser::editMimeType
void editMimeType()
edit the current mimetype Uses KRun to start the KDE mimetype editor for editing the currently select...
Definition: kmimetypechooser.cpp:190
KMimeTypeChooser::EditButton
Show the "Edit" button, allowing to edit the selected type.
Definition: kmimetypechooser.h:45
KMimeTypeChooserDialog::KMimeTypeChooserDialog
KMimeTypeChooserDialog(const TQString &caption=TQString::null, const TQString &text=TQString::null, const TQStringList &selectedMimeTypes=TQStringList(), const TQString &defaultGroup=TQString::null, const TQStringList &groupsToShow=TQStringList(), int visuals=KMimeTypeChooser::Comments|KMimeTypeChooser::Patterns|KMimeTypeChooser::EditButton, TQWidget *parent=0, const char *name=0)
Create a KMimeTypeChooser dialog.
Definition: kmimetypechooser.cpp:249
KMimeType::allMimeTypes
static List allMimeTypes()
Get all the mimetypes.
Definition: kmimetype.cpp:160
KRun::runCommand
static pid_t runCommand(TQString cmd)
Run the given shell command and notifies kicker of the starting of the application.
Definition: krun.cpp:848
KMimeTypeChooser
This widget provides a checkable list of all available mimetypes, and a list of selected ones...
Definition: kmimetypechooser.h:34
KMimeTypeChooser::mimeTypes
TQStringList mimeTypes() const
Definition: kmimetypechooser.cpp:217
KMimeTypeChooser::patterns
TQStringList patterns() const
Definition: kmimetypechooser.cpp:229

tdeio/tdeio

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

tdeio/tdeio

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