summaryrefslogtreecommitdiff
path: root/noncore/settings/networksettings2/networksettings2/systemfile.cpp
blob: 7249976cc5a74e1f998dcd953714c8e24e0a0a13 (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
#include <stdio.h>
#include <qpe/qpeapplication.h>
#include <qfileinfo.h>
#include <qmessagebox.h>
#include <qfile.h>
#include <qtextstream.h>

#include "resources.h"
#include "systemfile.h"

#define TEMPLATEDIR "networktemplates/"
QString TemplDir;

SystemFile::SystemFile( const QString & N, const QString & P ){
      Name = N;
      Path = P;
      F = 0;
      // get template info
      { QString S;
        QFileInfo FI;

        // find location of templates
        TemplDir = QPEApplication::qpeDir() + "etc/" + TEMPLATEDIR;
        FI.setFile( TemplDir );
        if( ! FI.isDir() ) {
          // try current dir
          TemplDir = "./" TEMPLATEDIR;
          FI.setFile( TemplDir );
          if( ! FI.isDir() ) {
            hasPreSection = 
              hasPostSection = 
              hasPreNodeSection = 
              hasPostNodeSection = 0;
            return;
          }
        }

        // have found location
        S = TemplDir + Name + "/presection";
        FI.setFile( S );
        hasPreSection = ( FI.exists() && FI.isReadable() );
        S = TemplDir + Name + "/postsection";
        FI.setFile( S );
        hasPostSection = ( FI.exists() && FI.isReadable() );
        S = TemplDir + Name + "/prenodesection";
        FI.setFile( S );
        hasPreNodeSection = ( FI.exists() && FI.isReadable() );
        S = TemplDir + Name + "/postnodesection";
        FI.setFile( S );
        hasPostNodeSection = ( FI.exists() && FI.isReadable() );
      }
}

SystemFile::~SystemFile( void ) {
      if( F )
        delete F;
}

bool SystemFile::open( void ) {
      if( F ) {
        F->close();
        delete F;
      }

      F = new QFile( Path + "bup" );
      if( ! F->open( IO_WriteOnly ) ) {
        return 0;
      }
      setDevice( F );
      return 1;
}

bool SystemFile::close( void ) {
      if( ! F ) {
        return 1 ;
      }

      QString OldP = Path + "bup";

      F->close();
      delete F;
      F = 0;

      return ( rename( OldP.latin1(), Path.latin1() ) >= 0 );
}

bool SystemFile::preSection( void ) {
      if( hasPreSection ) {
        QFile Fl( TemplDir + Name + "/presection" );
        if( ! Fl.open( IO_ReadOnly ) )
          return 1; // error
        // copy file to this file
        F->writeBlock( Fl.readAll() );
      }
      return 0;
}

bool SystemFile::postSection( void ) {
      if( hasPostSection ) {
        QFile Fl( TemplDir + Name + "/postsection" );
        if( ! Fl.open( IO_ReadOnly ) )
          return 1; // error
        // copy file to this file
        F->writeBlock( Fl.readAll() );
      }
      return 0;
}

bool SystemFile::preNodeSection( ANetNodeInstance * NNI, long ) {
      if( hasPreNodeSection ) {
        QFile Fl( TemplDir + Name + "/prenodesectoin" );
        if( ! Fl.open( IO_ReadOnly ) )
          return 1; // error
        QTextStream TX( &Fl );
        QString Out;
        QString S = TX.readLine();
        while( ! TX.eof() ) {
          Out = S.
              arg(NNI->netNode()->nodeName());
          (*this) << Out << endl;
          S = TX.readLine();
        }
      }
      return 0;
}

bool SystemFile::postNodeSection( ANetNodeInstance * NNI, long DevNr ) {
      if( hasPostNodeSection ) {
        QFile Fl( TemplDir + Name + "/postnodesectoin" );
        if( ! Fl.open( IO_ReadOnly ) )
          return 1; // error
        QTextStream TX( &Fl );
        QString Out;
        QString S = TX.readLine();
        while( ! TX.eof() ) {
          Out = S.
              arg(NNI->nodeName());
          (*this) << Out << endl;
          S = TX.readLine();
        }
      }
      return 0;
}