summaryrefslogtreecommitdiff
path: root/noncore/settings/networksettings2/networksettings2/resources.cpp
Unidiff
Diffstat (limited to 'noncore/settings/networksettings2/networksettings2/resources.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r--noncore/settings/networksettings2/networksettings2/resources.cpp236
1 files changed, 236 insertions, 0 deletions
diff --git a/noncore/settings/networksettings2/networksettings2/resources.cpp b/noncore/settings/networksettings2/networksettings2/resources.cpp
new file mode 100644
index 0000000..ff6e457
--- a/dev/null
+++ b/noncore/settings/networksettings2/networksettings2/resources.cpp
@@ -0,0 +1,236 @@
1#include <qpixmap.h>
2#include <qpe/qlibrary.h>
3#include <qpe/qpeapplication.h>
4#include <qdir.h>
5#include <qtopia/resource.h>
6
7#include "netnode.h"
8#include "resources.h"
9
10#define PLUGINDIR "plugins/networksettings2"
11#define ICONDIR "/pics/networksettings2/"
12
13// single resources instance
14TheNSResources * _NSResources = 0;
15
16TheNSResources::TheNSResources( void ) : NodeTypeNameMap(),
17 ConnectionsMap() {
18
19 _NSResources = this;
20
21 // load available netnodes
22 findAvailableNetNodes(QPEApplication::qpeDir() + PLUGINDIR );
23
24 // compile provides and needs lists
25 { const char ** NeedsRun;
26 QDictIterator<NetNode_t> OuterIt( AllNodeTypes );
27 bool Done;
28
29 for ( ; OuterIt.current(); ++OuterIt ) {
30 // find needs list
31 ANetNode::NetNodeList * NNLP = new ANetNode::NetNodeList;
32 ANetNode::NetNodeList & NNL = *(NNLP);
33
34 // must iterate this way to avoid duplication pointers
35 for ( QDictIterator<NetNode_t> InnerIt( AllNodeTypes );
36 InnerIt.current(); ++InnerIt ) {
37 if( InnerIt.current() == OuterIt.current() )
38 // avoid recursive
39 continue;
40
41 const char * Provides = InnerIt.current()->NetNode->provides();
42 NeedsRun = OuterIt.current()->NetNode->needs();
43 for( ; *NeedsRun; NeedsRun ++ ) {
44 if( strcmp( Provides, *NeedsRun ) == 0 ) {
45 // inner provides what outer needs
46 NNL.resize( NNL.size() + 1 );
47 NNL[NNL.size()-1] = InnerIt.current()->NetNode;
48 Done = 1; // break from 2 loops
49 break;
50 }
51 }
52 }
53 OuterIt.current()->NetNode->setAlternatives( NNLP );
54 }
55 }
56
57 // define Node types to Description map
58 NodeTypeNameMap.insert( "device", tr( "Network Device" ) );
59 NodeTypeNameMap.insert( "line", tr( "Character device" ) );
60 NodeTypeNameMap.insert( "connection", tr( "IP Connection" ) );
61 NodeTypeNameMap.insert( "fullsetup", tr( "Connection Profile" ) );
62
63 NodeTypeDescriptionMap.insert( "device",
64 tr( "<p>Devices that can handle IP packets</p>" ) );
65 NodeTypeDescriptionMap.insert( "line",
66 tr( "<p>Devices that can handle single bytes</p>" ) );
67 NodeTypeDescriptionMap.insert( "connection",
68 tr( "<p>Nodes that provide working IP connections</p>" ) );
69 NodeTypeDescriptionMap.insert( "fullsetup",
70 tr( "<p>Fully configured connection profile</p>" ) );
71
72 // define system files
73 addSystemFile( new SystemFile( "interfaces", "./interfaces" ) );
74
75 // get access to the system
76 TheSystem = new System();
77}
78
79TheNSResources::~TheNSResources( void ) {
80 delete TheSystem;
81}
82
83/**
84 * Load all modules that are found in the path
85 * @param path a directory that is scaned for any plugins that can be loaded
86 * and attempts to load them
87 */
88void TheNSResources::findAvailableNetNodes(const QString &path){
89
90 QDir d(path);
91 if(!d.exists())
92 return;
93
94 QString lang = ::getenv("LANG");
95
96 // Don't want sym links
97 d.setFilter( QDir::Files | QDir::NoSymLinks );
98 const QFileInfoList *list = d.entryInfoList();
99 QFileInfoListIterator it( *list );
100 QFileInfo *fi;
101
102 while ( (fi=it.current()) ) {
103
104 if( fi->fileName().contains(".so")){
105 /* if loaded install translation */
106 if( loadNetNode(path + "/" + fi->fileName()) ) {
107 QTranslator *trans = new QTranslator(qApp);
108 QString fn = QPEApplication::qpeDir()+
109 "/i18n/"+lang+"/"+
110 fi->fileName().left( fi->fileName().find(".") )+
111 ".qm";
112
113 if( trans->load( fn ) )
114 qApp->installTranslator( trans );
115 else
116 delete trans;
117 }
118 }
119 ++it;
120 }
121}
122
123/**
124 * Attempt to load a function and resolve a function.
125 * @param pluginFileName - the name of the file in which to attempt to load
126 * @param resolveString - function pointer to resolve
127 * @return true of loading is successful
128 */
129bool TheNSResources::loadNetNode(
130 const QString &pluginFileName, const QString &resolveString){
131
132 QLibrary *lib = new QLibrary(pluginFileName);
133 void * res = lib->resolve(resolveString);
134 if( ! res ){
135#ifdef DEBUG
136 qDebug("loadNetNode: Warning: %s is not a plugin", pluginFileName.latin1());
137#endif
138 delete lib;
139 return 0;
140 }
141
142 GetNetNodeListFt_t getNetNodeList = (GetNetNodeListFt_t)res;
143
144 // Try to get an object.
145 QList<ANetNode> PNN;
146
147 getNetNodeList( PNN );
148 if( PNN.isEmpty() ) {
149#ifdef DEBUG
150 qDebug("loadNetNode: Couldn't get node list, but did load library!");
151#endif
152 delete lib;
153 return 0;
154 }
155
156 ANetNode * NNP;
157 for( QListIterator<ANetNode> it(PNN);
158 it.current();
159 ++it ) {
160 NetNode_t * NN;
161
162 NNP = it.current();
163 NN = new NetNode_t;
164 NN->NetNode = NNP;
165 NN->TheLibrary = lib;
166 NN->NodeCountInLib = PNN.count();
167
168 // store mapping
169 AllNodeTypes.insert( NN->NetNode->nodeName(), NN );
170 }
171
172 return 1;
173}
174
175QPixmap TheNSResources::getPixmap( const QString & QS ) {
176 return Resource::loadPixmap( QString("networksettings2")+QS );
177}
178
179QString TheNSResources::tr( const char * s ) {
180 return qApp->translate( "resource", s );
181}
182
183const QString & TheNSResources::netNode2Name( const char * s ) {
184 return NodeTypeNameMap[s];
185}
186
187const QString & TheNSResources::netNode2Description( const char * s ) {
188 return NodeTypeDescriptionMap[s];
189}
190
191void TheNSResources::addConnection( NodeCollection * NC ) {
192 ANetNodeInstance * NNI;
193 ConnectionsMap.insert( NC->name(), NC );
194 // add (new) nodes to NodeList
195 for( QListIterator<ANetNodeInstance> it(*NC);
196 it.current();
197 ++it ) {
198 NNI = it.current();
199 if( findNodeInstance( NNI->nodeName() ) == 0 ) {
200 // new item
201 addNodeInstance( NNI );
202 }
203 }
204}
205
206void TheNSResources::removeConnection( const QString & N ) {
207 NodeCollection * NC = findConnection( N );
208 if( ! NC )
209 return;
210
211 // delete netnodes in this connection
212 ANetNodeInstance * NNI;
213 for( NNI = NC->first(); NNI != 0; NNI = NC->next() ) {
214 removeNodeInstance( NNI->nodeName() );
215 }
216 ConnectionsMap.remove( N );
217}
218
219NodeCollection * TheNSResources::findConnection( const QString & S ) {
220 return ConnectionsMap[ S ];
221}
222
223void TheNSResources::renumberConnections( void ) {
224 Name2Connection_t & M = NSResources->connections();
225 NodeCollection * NC;
226
227 // for all connections
228 NodeCollection::resetMaxNr();
229 for( QDictIterator<NodeCollection> it(M);
230 it.current();
231 ++it ) {
232 NC = it.current();
233 NC->setNumber( NC->maxConnectionNumber()+1 );
234 NC->setModified( 1 );
235 }
236}