summaryrefslogtreecommitdiffstats
path: root/kpovmodeler/pmdocumentationmap.cpp
blob: 40e23d24d3bdaf2675808161a12949e23e461f90 (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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/*
**************************************************************************
                                 description
                             --------------------
    copyright            : (C) 2002 by Andreas Zehender
    email                : [email protected]
**************************************************************************

**************************************************************************
*                                                                        *
*  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 "pmdocumentationmap.h"

#include <tdeconfig.h>
#include <kstandarddirs.h>

#include <tqfile.h>
#include <tqdom.h>

#include "pmdebug.h"

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

TQString PMDocumentationVersion::documentation( const TQString& className ) const
{
   if( m_map.contains( className ) )
      return m_map[className];
   return m_index;
}

void PMDocumentationVersion::loadData( TQDomElement& e )
{
   TQString className;
   TQString target;
   
   m_version = e.attribute( "number", "3.1" );
   m_index = e.attribute( "index", "index.htm" );

   TQDomNode m = e.firstChild( );
   while( !m.isNull( ) )
   {
      if( m.isElement( ) )
      {
         TQDomElement me = m.toElement( );
         className = me.attribute( "className", "" );
         target = me.attribute( "target", "" );
         if( !className.isEmpty( ) && !target.isEmpty( ) )
            m_map.insert( className, target );
      }
      m = m.nextSibling( );
   }
}

PMDocumentationMap::PMDocumentationMap( )
{
   m_pCurrentVersion = 0;
   m_mapLoaded = false;
}

PMDocumentationMap::~PMDocumentationMap( )
{
   m_maps.setAutoDelete( true );
   m_maps.clear( );
}

void PMDocumentationMap::saveConfig( TDEConfig* cfg )
{
   cfg->setGroup( "Povray" );
   cfg->writePathEntry( "DocumentationPath", m_documentationPath );
   cfg->writeEntry( "DocumentationVersion", m_currentVersion );
}

void PMDocumentationMap::restoreConfig( TDEConfig* cfg )
{
   cfg->setGroup( "Povray" );
   m_documentationPath = cfg->readPathEntry( "DocumentationPath" );
   m_currentVersion = cfg->readEntry( "DocumentationVersion", "3.1" );
}

void PMDocumentationMap::setDocumentationVersion( const TQString& v )
{
   m_currentVersion = v;
   if( m_mapLoaded )
      findVersion( );
}

TQValueList<TQString> PMDocumentationMap::availableVersions( )
{
   if( !m_mapLoaded )
      loadMap( );

   TQValueList<TQString> result;
   TQPtrListIterator<PMDocumentationVersion> it( m_maps );
   
   for( ; it.current( ); ++it )
      result.push_back( it.current( )->version( ) );
   
   return result;
}

TQString PMDocumentationMap::documentation( const TQString& objectName )
{
   if( !m_mapLoaded )
      loadMap( );
   
   TQString url;

   if( !m_documentationPath.isEmpty( ) )
      if( !m_documentationPath.endsWith( TQString( "/" ) ) )
         m_documentationPath += "/";
   
   if( !m_documentationPath.isEmpty( ) && m_pCurrentVersion )
      url = m_documentationPath
            + m_pCurrentVersion->documentation( objectName );

   return url;
}

void PMDocumentationMap::loadMap( )
{
   if( !m_mapLoaded )
   {
      m_mapLoaded = true;
      
      TQString fileName = locate( "data", "kpovmodeler/povraydocmap.xml" );
      if( fileName.isEmpty( ) )
      {
         kdError( PMArea ) << "Povray documentation map not found" << endl;
         return;
      }
      
      TQFile file( fileName );
      if( !file.open( IO_ReadOnly ) )
      {
         kdError( PMArea ) << "Could not open the povray documentation map file"
                           << endl;
         return;
      }

      TQDomDocument doc( "DOCMAP" );
      doc.setContent( &file );

      TQDomElement e = doc.documentElement( );
      TQDomNode c = e.firstChild( );

      TQString str;

      while( !c.isNull( ) )
      {
         if( c.isElement( ) )
         {
            TQDomElement ce = c.toElement( );
            PMDocumentationVersion* v = new PMDocumentationVersion( );
            m_maps.append( v );
            v->loadData( ce );
         }
         c = c.nextSibling( );
      }

      findVersion( );
   }
}

void PMDocumentationMap::findVersion( )
{
   TQPtrListIterator< PMDocumentationVersion > it( m_maps );
   bool found = false;

   m_pCurrentVersion = 0;

   for( ; it.current( ) && !found; ++it )
   {
      if( it.current( )->version( ) == m_currentVersion )
      {
         found = true;
         m_pCurrentVersion = it.current( );
      }
   }
}

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