TDE core libraries
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

169 lines
4.2KB

  1. /*
  2. This file is part of libtdeabc.
  3. Copyright (c) 2002 Tobias Koenig <tokoe@kde.org>
  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 as published by the Free Software Foundation; either
  7. version 2 of the License, or (at your option) any later version.
  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. You should have received a copy of the GNU Library General Public License
  13. along with this library; see the file COPYING.LIB. If not, write to
  14. the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  15. Boston, MA 02110-1301, USA.
  16. */
  17. #include <kdebug.h>
  18. #include <tdelocale.h>
  19. #include <ksimpleconfig.h>
  20. #include <kstandarddirs.h>
  21. #include <kstaticdeleter.h>
  22. #include <tqfile.h>
  23. #include "vcardformatplugin.h"
  24. #include "formatfactory.h"
  25. using namespace TDEABC;
  26. FormatFactory *FormatFactory::mSelf = 0;
  27. static KStaticDeleter<FormatFactory> factoryDeleter;
  28. FormatFactory *FormatFactory::self()
  29. {
  30. kdDebug(5700) << "FormatFactory::self()" << endl;
  31. if ( !mSelf )
  32. factoryDeleter.setObject( mSelf, new FormatFactory );
  33. return mSelf;
  34. }
  35. FormatFactory::FormatFactory()
  36. {
  37. mFormatList.setAutoDelete( true );
  38. // dummy entry for default format
  39. FormatInfo *info = new FormatInfo;
  40. info->library = "<NoLibrary>";
  41. info->nameLabel = i18n( "vCard" );
  42. info->descriptionLabel = i18n( "vCard Format" );
  43. mFormatList.insert( "vcard", info );
  44. const TQStringList list = TDEGlobal::dirs()->findAllResources( "data" ,"tdeabc/formats/*.desktop", true, true );
  45. for ( TQStringList::ConstIterator it = list.begin(); it != list.end(); ++it )
  46. {
  47. KSimpleConfig config( *it, true );
  48. if ( !config.hasGroup( "Misc" ) || !config.hasGroup( "Plugin" ) )
  49. continue;
  50. info = new FormatInfo;
  51. config.setGroup( "Plugin" );
  52. TQString type = config.readEntry( "Type" );
  53. info->library = config.readEntry( "X-TDE-Library" );
  54. config.setGroup( "Misc" );
  55. info->nameLabel = config.readEntry( "Name" );
  56. info->descriptionLabel = config.readEntry( "Comment", i18n( "No description available." ) );
  57. mFormatList.insert( type, info );
  58. }
  59. }
  60. FormatFactory::~FormatFactory()
  61. {
  62. mFormatList.clear();
  63. }
  64. TQStringList FormatFactory::formats()
  65. {
  66. TQStringList retval;
  67. // make sure 'vcard' is the first entry
  68. retval << "vcard";
  69. TQDictIterator<FormatInfo> it( mFormatList );
  70. for ( ; it.current(); ++it )
  71. if ( it.currentKey() != "vcard" )
  72. retval << it.currentKey();
  73. return retval;
  74. }
  75. FormatInfo *FormatFactory::info( const TQString &type )
  76. {
  77. if ( type.isEmpty() )
  78. return 0;
  79. else
  80. return mFormatList[ type ];
  81. }
  82. FormatPlugin *FormatFactory::format( const TQString& type )
  83. {
  84. FormatPlugin *format = 0;
  85. if ( type.isEmpty() )
  86. return 0;
  87. if ( type == "vcard" ) {
  88. format = new VCardFormatPlugin;
  89. format->setType( type );
  90. format->setNameLabel( i18n( "vCard" ) );
  91. format->setDescriptionLabel( i18n( "vCard Format" ) );
  92. return format;
  93. }
  94. FormatInfo *fi = mFormatList[ type ];
  95. if (!fi)
  96. return 0;
  97. TQString libName = fi->library;
  98. KLibrary *library = openLibrary( libName );
  99. if ( !library )
  100. return 0;
  101. void *format_func = library->symbol( "format" );
  102. if ( format_func ) {
  103. format = ((FormatPlugin* (*)())format_func)();
  104. format->setType( type );
  105. format->setNameLabel( fi->nameLabel );
  106. format->setDescriptionLabel( fi->descriptionLabel );
  107. } else {
  108. kdDebug( 5700 ) << "'" << libName << "' is not a format plugin." << endl;
  109. return 0;
  110. }
  111. return format;
  112. }
  113. KLibrary *FormatFactory::openLibrary( const TQString& libName )
  114. {
  115. KLibrary *library = 0;
  116. TQString path = KLibLoader::findLibrary( TQFile::encodeName( libName ) );
  117. if ( path.isEmpty() ) {
  118. kdDebug( 5700 ) << "No format plugin library was found!" << endl;
  119. return 0;
  120. }
  121. library = KLibLoader::self()->library( TQFile::encodeName( path ) );
  122. if ( !library ) {
  123. kdDebug( 5700 ) << "Could not load library '" << libName << "'" << endl;
  124. return 0;
  125. }
  126. return library;
  127. }