• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdelibs-4.10.5 API Reference
  • KDE Home
  • Contact Us
 

KDECore

  • kdecore
  • services
kmimetypefactory.cpp
Go to the documentation of this file.
1 /* This file is part of the KDE libraries
2  * Copyright (C) 1999 Waldo Bastian <bastian@kde.org>
3  * Copyright (C) 2006-2009 David Faure <faure@kde.org>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Library General Public
7  * License version 2 as published by the Free Software Foundation;
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public License
15  * along with this library; see the file COPYING.LIB. If not, write to
16  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17  * Boston, MA 02110-1301, USA.
18  */
19 
20 #include "kmimetypefactory.h"
21 #include <ksycoca.h>
22 #include <ksycocadict_p.h>
23 #include <kdebug.h>
24 
25 extern int servicesDebugArea();
26 
27 K_GLOBAL_STATIC(KSycocaFactorySingleton<KMimeTypeFactory>, kMimeTypeFactoryInstance)
28 
29 KMimeTypeFactory::KMimeTypeFactory()
30  : KSycocaFactory( KST_KMimeTypeFactory )
31 {
32  kMimeTypeFactoryInstance->instanceCreated(this);
33 }
34 
35 KMimeTypeFactory::~KMimeTypeFactory()
36 {
37  if (kMimeTypeFactoryInstance.exists())
38  kMimeTypeFactoryInstance->instanceDestroyed(this);
39 }
40 
41 KMimeTypeFactory * KMimeTypeFactory::self()
42 {
43  return kMimeTypeFactoryInstance->self();
44 }
45 
46 int KMimeTypeFactory::entryOffset(const QString& mimeTypeName)
47 {
48  if (!sycocaDict())
49  return -1; // Error!
50  assert (!KSycoca::self()->isBuilding());
51  const int offset = sycocaDict()->find_string(mimeTypeName);
52  return offset;
53 }
54 
55 int KMimeTypeFactory::serviceOffersOffset(const QString& mimeTypeName)
56 {
57  const int offset = entryOffset(mimeTypeName);
58  if (!offset)
59  return -1; // Not found
60 
61  MimeTypeEntry::Ptr newMimeType(createEntry(offset));
62  if (!newMimeType)
63  return -1;
64  // Check whether the dictionary was right.
65  if (newMimeType->name() != mimeTypeName) {
66  // No it wasn't...
67  return -1;
68  }
69  return newMimeType->serviceOffersOffset();
70 }
71 
72 KMimeTypeFactory::MimeTypeEntry * KMimeTypeFactory::createEntry(int offset) const
73 {
74  MimeTypeEntry *newEntry = 0;
75  KSycocaType type;
76  QDataStream *str = KSycoca::self()->findEntry(offset, type);
77  if (!str) return 0;
78 
79  switch(type)
80  {
81  case KST_KMimeTypeEntry:
82  newEntry = new MimeTypeEntry(*str, offset);
83  break;
84 
85  // Old, now unused
86  case KST_KMimeType:
87  case KST_KFolderMimeType:
88  return 0;
89 
90  default:
91  kError(7011) << "KMimeTypeFactory: unexpected object entry in KSycoca database (type=" << int(type) << ")";
92  break;
93  }
94  if (newEntry && !newEntry->isValid())
95  {
96  kError(7011) << "KMimeTypeFactory: corrupt object in KSycoca database!\n";
97  delete newEntry;
98  newEntry = 0;
99  }
100  return newEntry;
101 }
102 
103 QStringList KMimeTypeFactory::allMimeTypes()
104 {
105  // TODO: reimplement in terms of "listing xdgdata-mime", to avoid ksycoca dependency,
106  // then move to KMimeTypeRepository
107  QStringList result;
108  const KSycocaEntry::List list = allEntries();
109  for( KSycocaEntry::List::ConstIterator it = list.begin();
110  it != list.end();
111  ++it)
112  {
113  Q_ASSERT( (*it)->isType( KST_KMimeTypeEntry ) );
114  result.append( MimeTypeEntry::Ptr::staticCast( *it )->name() );
115  }
116  return result;
117 }
118 
120 
121 class KMimeTypeFactory::MimeTypeEntryPrivate : public KSycocaEntryPrivate
122 {
123 public:
124  K_SYCOCATYPE( KST_KMimeTypeEntry, KSycocaEntryPrivate )
125  MimeTypeEntryPrivate(const QString& file, const QString& name)
126  : KSycocaEntryPrivate(file), m_name(name), m_serviceOffersOffset(-1)
127  {
128  }
129  MimeTypeEntryPrivate(QDataStream& s, int offset)
130  : KSycocaEntryPrivate(s, offset), m_serviceOffersOffset(-1)
131  {
132  s >> m_name >> m_serviceOffersOffset;
133  }
134  virtual QString name() const { return m_name; }
135  virtual void save(QDataStream &s) {
136  KSycocaEntryPrivate::save(s);
137  s << m_name << m_serviceOffersOffset;
138  }
139 
140  QString m_name;
141  int m_serviceOffersOffset;
142 };
143 
144 KMimeTypeFactory::MimeTypeEntry::MimeTypeEntry(const QString& file, const QString& name)
145  : KSycocaEntry(*new MimeTypeEntryPrivate(file, name))
146 {
147 }
148 
149 KMimeTypeFactory::MimeTypeEntry::MimeTypeEntry(QDataStream& s, int offset)
150  : KSycocaEntry(*new MimeTypeEntryPrivate(s, offset))
151 {
152 }
153 
154 int KMimeTypeFactory::MimeTypeEntry::serviceOffersOffset() const
155 {
156  Q_D(const MimeTypeEntry);
157  return d->m_serviceOffersOffset;
158 }
159 
160 void KMimeTypeFactory::MimeTypeEntry::setServiceOffersOffset(int off)
161 {
162  Q_D(MimeTypeEntry);
163  d->m_serviceOffersOffset = off;
164 }
This file is part of the KDE documentation.
Documentation copyright © 1996-2013 The KDE developers.
Generated on Tue Jul 23 2013 20:27:53 by doxygen 1.8.1.1 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

KDECore

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

kdelibs-4.10.5 API Reference

Skip menu "kdelibs-4.10.5 API Reference"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDEWebKit
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal