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

tdeio/tdeio

  • tdeio
  • tdeio
kuserprofile.cpp
1 /* This file is part of the KDE libraries
2  * Copyright (C) 1999 Torben Weis <weis@kde.org>
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 "kuserprofile.h"
20 #include "kservice.h"
21 #include "kservicetype.h"
22 #include "kservicetypefactory.h"
23 
24 #include <tdeconfig.h>
25 #include <tdeapplication.h>
26 #include <tdeglobal.h>
27 #include <kdebug.h>
28 #include <kstaticdeleter.h>
29 
30 #include <tqtl.h>
31 
32 template class TQPtrList<KServiceTypeProfile>;
33 typedef TQPtrList<KServiceTypeProfile> KServiceTypeProfileList;
34 
35 /*********************************************
36  *
37  * KServiceTypeProfile
38  *
39  *********************************************/
40 
41 KServiceTypeProfileList* KServiceTypeProfile::s_lstProfiles = 0L;
42 static KStaticDeleter< KServiceTypeProfileList > profileDeleter;
43 bool KServiceTypeProfile::s_configurationMode = false;
44 
45 void KServiceTypeProfile::initStatic()
46 {
47  if ( s_lstProfiles )
48  return;
49 
50  // Make sure that a KServiceTypeFactory gets created.
51  (void) KServiceTypeFactory::self();
52 
53  profileDeleter.setObject(s_lstProfiles, new KServiceTypeProfileList);
54  s_lstProfiles->setAutoDelete( true );
55 
56  TDEConfig config( "profilerc", true, false);
57 
58  static const TQString & defaultGroup = TDEGlobal::staticQString("<default>");
59 
60  TQStringList tmpList = config.groupList();
61  for (TQStringList::Iterator aIt = tmpList.begin();
62  aIt != tmpList.end(); ++aIt) {
63  if ( *aIt == defaultGroup )
64  continue;
65 
66  config.setGroup( *aIt );
67 
68  TQString appId = config.readEntry( "Application" );
69 
70  KService::Ptr pService = KService::serviceByStorageId(appId);
71 
72  if ( pService ) {
73  TQString application = pService->storageId();
74  TQString type = config.readEntry( "ServiceType" );
75  TQString type2 = config.readEntry( "GenericServiceType" );
76  if (type2.isEmpty()) // compat code
77  type2 = (pService->type() == "Application") ? "Application" : "KParts/ReadOnlyPart";
78  int pref = config.readNumEntry( "Preference" );
79 
80  if ( !type.isEmpty() /* && pref >= 0*/ ) // Don't test for pref here. We want those in the list, to mark them as forbidden
81  {
82  KServiceTypeProfile* p =
83  KServiceTypeProfile::serviceTypeProfile( type, type2 );
84 
85  if ( !p ) {
86  p = new KServiceTypeProfile( type, type2 );
87  s_lstProfiles->append( p );
88  }
89 
90  bool allow = config.readBoolEntry( "AllowAsDefault" );
91  //kdDebug(7014) << "KServiceTypeProfile::initStatic adding service " << application << " to profile for " << type << "," << type2 << " with preference " << pref << endl;
92  p->addService( application, pref, allow );
93  }
94  }
95  }
96 }
97 
98 //static
99 void KServiceTypeProfile::clear()
100 {
101  // HACK tdesycoca may open the dummy db, in such case the first call to tdesycoca
102  // in initStatic() leads to closing the dummy db and clear() being called
103  // in the middle of it, making s_lstProfiles be NULL
104  if( s_lstProfiles == NULL || s_lstProfiles->count() == 0 )
105  return;
106  profileDeleter.destructObject();
107 }
108 
109 //static
110 KServiceTypeProfile::OfferList KServiceTypeProfile::offers( const TQString& _servicetype, const TQString& _genericServiceType )
111 {
112  OfferList offers;
113  TQStringList serviceList;
114  //kdDebug(7014) << "KServiceTypeProfile::offers( " << _servicetype << "," << _genericServiceType << " )" << endl;
115 
116  // Note that KServiceTypeProfile::offers() calls KServiceType::offers(),
117  // so we _do_ get the new services, that are available but not in the profile.
118  if ( _genericServiceType.isEmpty() )
119  {
120  initStatic();
121  // We want all profiles for servicetype, if we have profiles.
122  // ## Slow loop, if profilerc is big. We should use a map instead?
123  TQPtrListIterator<KServiceTypeProfile> it( *s_lstProfiles );
124  for( ; it.current(); ++it )
125  if ( it.current()->m_strServiceType == _servicetype )
126  {
127  offers += it.current()->offers();
128  }
129  //kdDebug(7014) << "Found profile: " << offers.count() << " offers" << endl;
130  }
131  else
132  {
133  KServiceTypeProfile* profile = serviceTypeProfile( _servicetype, _genericServiceType );
134  if ( profile )
135  {
136  //kdDebug(7014) << "Found profile: " << profile->offers().count() << " offers" << endl;
137  offers += profile->offers();
138  }
139  else
140  {
141  // Try the other way round, order is not like size, it doesn't matter.
142  profile = serviceTypeProfile( _genericServiceType, _servicetype );
143  if ( profile )
144  {
145  //kdDebug(7014) << "Found profile after switching: " << profile->offers().count() << " offers" << endl;
146  offers += profile->offers();
147  }
148  }
149  }
150 
151  // Collect services, to make the next loop faster
152  OfferList::Iterator itOffers = offers.begin();
153  for( ; itOffers != offers.end(); ++itOffers )
154  serviceList += (*itOffers).service()->desktopEntryPath(); // this should identify each service uniquely
155  //kdDebug(7014) << "serviceList: " << serviceList.join(",") << endl;
156 
157  // Now complete with any other offers that aren't in the profile
158  // This can be because the services have been installed after the profile was written,
159  // but it's also the case for any service that's neither App nor ReadOnlyPart, e.g. RenameDlg/Plugin
160  KService::List list = KServiceType::offers( _servicetype );
161  //kdDebug(7014) << "Using KServiceType::offers, result: " << list.count() << " offers" << endl;
162  TQValueListIterator<KService::Ptr> it = list.begin();
163  for( ; it != list.end(); ++it )
164  {
165  if (_genericServiceType.isEmpty() /*no constraint*/ || (*it)->hasServiceType( _genericServiceType ))
166  {
167  // Check that we don't already have it ;)
168  if ( serviceList.find( (*it)->desktopEntryPath() ) == serviceList.end() )
169  {
170  bool allow = (*it)->allowAsDefault();
171  KServiceOffer o( (*it), (*it)->initialPreferenceForMimeType(_servicetype), allow );
172  offers.append( o );
173  //kdDebug(7014) << "Appending offer " << (*it)->name() << " initial preference=" << (*it)->initialPreference() << " allow-as-default=" << allow << endl;
174  }
175  //else
176  // kdDebug(7014) << "Already having offer " << (*it)->name() << endl;
177  }
178  }
179 
180  qBubbleSort( offers );
181 
182 #if 0
183  // debug code, comment if you wish but don't remove.
184  kdDebug(7014) << "Sorted list:" << endl;
185  OfferList::Iterator itOff = offers.begin();
186  for( ; itOff != offers.end(); ++itOff )
187  kdDebug(7014) << (*itOff).service()->name() << " allow-as-default=" << (*itOff).allowAsDefault() << endl;
188 #endif
189 
190  //kdDebug(7014) << "Returning " << offers.count() << " offers" << endl;
191  return offers;
192 }
193 
194 KServiceTypeProfile::KServiceTypeProfile( const TQString& _servicetype, const TQString& _genericServiceType )
195 {
196  initStatic();
197 
198  m_strServiceType = _servicetype;
199  m_strGenericServiceType = _genericServiceType;
200 }
201 
202 KServiceTypeProfile::~KServiceTypeProfile()
203 {
204 }
205 
206 void KServiceTypeProfile::addService( const TQString& _service,
207  int _preference, bool _allow_as_default )
208 {
209  m_mapServices[ _service ].m_iPreference = _preference;
210  m_mapServices[ _service ].m_bAllowAsDefault = _allow_as_default;
211 }
212 
213 int KServiceTypeProfile::preference( const TQString& _service ) const
214 {
215  KService::Ptr service = KService::serviceByName( _service );
216  if (!service)
217  return 0;
218  TQMap<TQString,Service>::ConstIterator it = m_mapServices.find( service->storageId() );
219  if ( it == m_mapServices.end() )
220  return 0;
221 
222  return it.data().m_iPreference;
223 }
224 
225 bool KServiceTypeProfile::allowAsDefault( const TQString& _service ) const
226 {
227  KService::Ptr service = KService::serviceByName( _service );
228  if (!service)
229  return false;
230 
231  // Does the service itself not allow that ?
232  if ( !service->allowAsDefault() )
233  return false;
234 
235  // Look what the user says ...
236  TQMap<TQString,Service>::ConstIterator it = m_mapServices.find( service->storageId() );
237  if ( it == m_mapServices.end() )
238  return 0;
239 
240  return it.data().m_bAllowAsDefault;
241 }
242 
243 KServiceTypeProfile* KServiceTypeProfile::serviceTypeProfile( const TQString& _servicetype, const TQString& _genericServiceType )
244 {
245  initStatic();
246  static const TQString& app_str = TDEGlobal::staticQString("Application");
247 
248  const TQString &_genservicetype = ((!_genericServiceType.isEmpty()) ? _genericServiceType : app_str);
249 
250  TQPtrListIterator<KServiceTypeProfile> it( *s_lstProfiles );
251  for( ; it.current(); ++it )
252  if (( it.current()->m_strServiceType == _servicetype ) &&
253  ( it.current()->m_strGenericServiceType == _genservicetype))
254  return it.current();
255 
256  return 0;
257 }
258 
259 
260 KServiceTypeProfile::OfferList KServiceTypeProfile::offers() const
261 {
262  OfferList offers;
263 
264  kdDebug(7014) << "KServiceTypeProfile::offers serviceType=" << m_strServiceType << " genericServiceType=" << m_strGenericServiceType << endl;
265  KService::List list = KServiceType::offers( m_strServiceType );
266  TQValueListIterator<KService::Ptr> it = list.begin();
267  for( ; it != list.end(); ++it )
268  {
269  //kdDebug(7014) << "KServiceTypeProfile::offers considering " << (*it)->name() << endl;
270  if ( m_strGenericServiceType.isEmpty() || (*it)->hasServiceType( m_strGenericServiceType ) )
271  {
272  // Now look into the profile, to find this service's preference.
273  TQMap<TQString,Service>::ConstIterator it2 = m_mapServices.find( (*it)->storageId() );
274 
275  if( it2 != m_mapServices.end() )
276  {
277  //kdDebug(7014) << "found in mapServices pref=" << it2.data().m_iPreference << endl;
278  if ( it2.data().m_iPreference > 0 ) {
279  bool allow = (*it)->allowAsDefault();
280  if ( allow )
281  allow = it2.data().m_bAllowAsDefault;
282  KServiceOffer o( (*it), it2.data().m_iPreference, allow );
283  offers.append( o );
284  }
285  }
286  else
287  {
288  //kdDebug(7014) << "not found in mapServices. Appending." << endl;
289  // We use 0 as the preference to ensure new apps don't take over existing apps (which default to 1)
290  KServiceOffer o( (*it), 0, (*it)->allowAsDefault() );
291  offers.append( o );
292  }
293  }/* else
294  kdDebug(7014) << "Doesn't have " << m_strGenericServiceType << endl;*/
295  }
296 
297  qBubbleSort( offers );
298 
299  //kdDebug(7014) << "KServiceTypeProfile::offers returning " << offers.count() << " offers" << endl;
300  return offers;
301 }
302 
303 KService::Ptr KServiceTypeProfile::preferredService( const TQString & _serviceType, const TQString & _genericServiceType )
304 {
305  OfferList lst = offers( _serviceType, _genericServiceType );
306 
307  OfferList::Iterator itOff = lst.begin();
308  // Look for the first one that is allowed as default.
309  // Since the allowed-as-default are first anyway, we only have
310  // to look at the first one to know.
311  if( itOff != lst.end() && (*itOff).allowAsDefault() )
312  return (*itOff).service();
313 
314  //kdDebug(7014) << "No offers, or none allowed as default" << endl;
315  return 0L;
316 }
317 
318 /*********************************************
319  *
320  * KServiceOffer
321  *
322  *********************************************/
323 
324 KServiceOffer::KServiceOffer()
325 {
326  m_iPreference = -1;
327 }
328 
329 KServiceOffer::KServiceOffer( const KServiceOffer& _o )
330 {
331  m_pService = _o.m_pService;
332  m_iPreference = _o.m_iPreference;
333  m_bAllowAsDefault = _o.m_bAllowAsDefault;
334 }
335 
336 KServiceOffer::KServiceOffer( KService::Ptr _service, int _pref, bool _default )
337 {
338  m_pService = _service;
339  m_iPreference = _pref;
340  m_bAllowAsDefault = _default;
341 }
342 
343 
344 bool KServiceOffer::operator< ( const KServiceOffer& _o ) const
345 {
346  // Put offers allowed as default FIRST.
347  if ( _o.m_bAllowAsDefault && !m_bAllowAsDefault )
348  return false; // _o is default and not 'this'.
349  if ( !_o.m_bAllowAsDefault && m_bAllowAsDefault )
350  return true; // 'this' is default but not _o.
351  // Both offers are allowed or not allowed as default
352  // -> use preferences to sort them
353  // The bigger the better, but we want the better FIRST
354  return _o.m_iPreference < m_iPreference;
355 }
KServiceTypeProfile::addService
void addService(const TQString &_service, int _preference=1, bool _allow_as_default=true)
Add a service to this profile.
Definition: kuserprofile.cpp:206
KServiceTypeProfile::offers
OfferList offers() const
Returns the list of all service offers for the service types that are represented by this profile...
Definition: kuserprofile.cpp:260
KService::serviceByStorageId
static Ptr serviceByStorageId(const TQString &_storageId)
Find a service by its storage-id or desktop-file path.
Definition: kservice.cpp:694
KService::serviceByName
static Ptr serviceByName(const TQString &_name)
Find a service by name, i.e.
Definition: kservice.cpp:668
KServiceTypeProfile::preference
int preference(const TQString &_service) const
Definition: kuserprofile.cpp:213
KServiceTypeProfile
KServiceTypeProfile represents the user's preferences for services of a service type.
Definition: kuserprofile.h:117
KServiceType::offers
static KService::List offers(const TQString &_servicetype)
Returns all services supporting the given servicetype name.
Definition: kservicetype.cpp:251
KServiceOffer::KServiceOffer
KServiceOffer()
Create an invalid service offer.
Definition: kuserprofile.cpp:324
KServiceTypeProfile::allowAsDefault
bool allowAsDefault(const TQString &_service) const
Definition: kuserprofile.cpp:225
KServiceOffer
This class holds the user-specific preferences of a service (whether it can be a default offer or not...
Definition: kuserprofile.h:40
KServiceTypeProfile::serviceTypeProfile
static KServiceTypeProfile * serviceTypeProfile(const TQString &servicetype, const TQString &genericServiceType=TQString::null)
Returns the profile for the requested service type.
Definition: kuserprofile.cpp:243
KServiceOffer::operator<
bool operator<(const KServiceOffer &) const
A service is bigger that the other when it can be default (and the other is not) and its preference v...
Definition: kuserprofile.cpp:344
KServiceTypeProfile::preferredService
static KService::Ptr preferredService(const TQString &serviceType, const TQString &genericServiceType)
Returns the preferred service for _serviceType and _genericServiceType ("Application", type of component, or null).
Definition: kuserprofile.cpp:303
KServiceTypeProfile::clear
static void clear()
Clear all cached information.
Definition: kuserprofile.cpp:99
KServiceTypeProfile::KServiceTypeProfile
KServiceTypeProfile(const TQString &serviceType, const TQString &genericServiceType=TQString::null)
Constructor is called when the user profile is read for the first time.
Definition: kuserprofile.cpp:194

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.