summaryrefslogtreecommitdiffstats
path: root/kpovmodeler/pmlibrarymanager.cpp
blob: cf9c6f961e7925b3c8a96398d814532bd9140f1f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/*
**************************************************************************
                                 description
                             --------------------
    copyright            : (C) 2003 by Luis Carvalho
    email                : lpassos@oninetspeed.pt
**************************************************************************

**************************************************************************
*                                                                        *
*  This program is free software; you can redistribute it and/or modify  *
*  it under the terms of the GNU General Public License as published by  *
*  the Free Software Foundation; either version 2 of the License, or     *
*  (at your option) any later version.                                   *
*                                                                        *
**************************************************************************/

#include "pmlibrarymanager.h"

#include <kconfig.h>
#include <kstandarddirs.h>
#include <kglobal.h>

#include <tqfile.h>
#include <tqdir.h>

#include "pmdebug.h"

PMLibraryManager* PMLibraryManager::s_pInstance = 0;
KStaticDeleter<PMLibraryManager> PMLibraryManager::s_staticDeleter;

PMLibraryHandle* PMLibraryManager::getLibraryHandle( const TQString& libraryName ) 
{
   TQPtrListIterator<PMLibraryHandle> it( m_libraries );

   for( ; it.current( ); ++it )
      if( it.current( )->name( ) == libraryName )
         return it.current( );

   return NULL;
}

PMLibraryManager::PMLibraryManager( )
{
   m_libraries.setAutoDelete( true );
   scanLibraries( );
}

PMLibraryManager::~PMLibraryManager( )
{
   m_libraries.clear( );
}

void PMLibraryManager::saveConfig( TDEConfig* /*cfg*/ )
{
}

void PMLibraryManager::restoreConfig( TDEConfig* /*cfg*/ )
{
}

TQValueList<TQString> PMLibraryManager::availableLibraries( )
{
   TQValueList<TQString> result;
   TQPtrListIterator<PMLibraryHandle> it( m_libraries );

   for( ; it.current( ); ++it )
      result.push_back( it.current( )->name( ) );

   return result;
}

PMLibraryManager* PMLibraryManager::theManager( )
{
   if( !s_pInstance )
      s_staticDeleter.setObject( s_pInstance, new PMLibraryManager( ) );
   return s_pInstance;
}

void PMLibraryManager::scanLibraries( )
{
   TQStringList libraryDirectories;
   
   // Search for sub directories in /usr/share/kpovmodeler/library
   libraryDirectories = TDEGlobal::dirs( )->findDirs( "data", "kpovmodeler/library" );

   for( TQStringList::Iterator i = libraryDirectories.begin( ); i != libraryDirectories.end( ); ++i )
   {
      TQDir curDir( *i );
      curDir.setFilter( TQDir::Dirs );
      TQFileInfoListIterator it( *( curDir.entryInfoList( ) ) );

      // For each sub directory
      TQFileInfo* fi;
      for( ; ( fi = it.current( ) ) != NULL; ++it ) 
      {
         // check for the existance of library_index.xml
         // If it's there it's a library
         if( TQFile::exists( fi->absFilePath( ) + "/library_index.xml" ) )
         {
            // Create the corresponding PMLibraryHandle
            PMLibraryHandle* h;

            h = new PMLibraryHandle( fi->absFilePath( ) );
            if( !getLibraryHandle( h->name( ) ) )
               m_libraries.append( h );
            else
               // a library with that name already exists
               delete h;
         }
      }
   }
}

void PMLibraryManager::refresh( )
{
   // TODO: Manage the list incrementaly so that previouly handed out 
   // PMLibraryHandle pointers are kept valid
   m_libraries.clear( );
   scanLibraries( );
}