summaryrefslogtreecommitdiffstats
path: root/libkscan/kscanoptset.cpp
blob: eac4498747c9eddcf3bedf56baf69c1adac0d11f (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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
/* This file is part of the KDE Project
   Copyright (C) 2000 Klaas Freitag <freitag@suse.de>

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

   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public License
   along with this library; see the file COPYING.LIB.  If not, write to
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
   Boston, MA 02110-1301, USA.
*/

#include <tqstring.h>
#include <tqasciidict.h>
#include <tqmap.h>
#include <tqdict.h>
#include <kdebug.h>
#include <kconfig.h>

#include "kscandevice.h"
#include "kscanoption.h"
#include "kscanoptset.h"

KScanOptSet::KScanOptSet( const TQCString& setName )
{
  name = setName;

  setAutoDelete( false );

  description = "";

  strayCatsList.setAutoDelete( true );
}



KScanOptSet::~KScanOptSet()
{
   /* removes all deep copies from backupOption */
   strayCatsList.clear();
}



KScanOption *KScanOptSet::get( const TQCString name ) const
{
  KScanOption *ret = 0;

  ret = (*this) [name];

  return( ret );
}

TQCString KScanOptSet::getValue( const TQCString name ) const
{
   KScanOption *re = get( name );
   TQCString retStr = "";

   if( re )
   {
      retStr = re->get();
   }
   else
   {
      kdDebug(29000) << "option " << name << " from OptionSet is not available" << endl;
   }
   return( retStr );
}


bool KScanOptSet::backupOption( const KScanOption& opt )
{
  bool retval = true;

  /** Allocate a new option and store it **/
  const TQCString& optName = opt.getName();
  if( !optName )
    retval = false;

  if( retval )
  {
     KScanOption *newopt = find( optName );

     if( newopt )
     {
	/** The option already exists **/
	/* Copy the new one into the old one. TODO: checken Zuweisungoperatoren OK ? */
	*newopt = opt;
     }
     else
     {
	const TQCString& qq = opt.get();
	kdDebug(29000) << "Value is now: <" << qq << ">" << endl;
	const KScanOption *newopt = new KScanOption( opt );

	strayCatsList.append( newopt );

	if( newopt )
	{
	   insert( optName, newopt );
	} else {
	   retval = false;
	}
     }
  }

  return( retval );

}

TQString KScanOptSet::getDescription() const
{
   return description;
}

void KScanOptSet::slSetDescription( const TQString& str )
{
   description = str;
}

void KScanOptSet::backupOptionDict( const TQAsciiDict<KScanOption>& optDict )
{
   TQAsciiDictIterator<KScanOption> it( optDict );

   while ( it.current() )
   {
      kdDebug(29000) << "Dict-Backup of Option <" << it.currentKey() << ">" << endl;
      backupOption( *(it.current()));
      ++it;
   }


}

/* */
void KScanOptSet::saveConfig( const TQString& scannerName, const TQString& configName,
			      const TQString& descr )
{
   TQString confFile = SCANNER_DB_FILE;
   kdDebug( 29000) << "Creating scan configuration file <" << confFile << ">" << endl;

   KConfig *scanConfig = new KConfig( confFile );
   TQString cfgName = configName;

   if( configName.isNull() || configName.isEmpty() )
      cfgName = "default";

   scanConfig->setGroup( cfgName );

   scanConfig->writeEntry( "description", descr );
   scanConfig->writeEntry( "scannerName", scannerName );
   TQAsciiDictIterator<KScanOption> it( *this);

    while ( it.current() )
    {
       const TQString line = it.current() -> configLine();
       const TQString name = it.current()->getName();

       kdDebug(29000) << "writing " << name << " = <" << line << ">" << endl;

       scanConfig->writeEntry( name, line );

       ++it;
    }

    scanConfig->sync();
    delete( scanConfig );
}

bool KScanOptSet::load( const TQString& /*scannerName*/ )
{
   TQString confFile = SCANNER_DB_FILE;
   kdDebug( 29000) << "** Reading from scan configuration file <" << confFile << ">" << endl;
   bool ret = true;

   KConfig *scanConfig = new KConfig( confFile, true );
   TQString cfgName = name; /* of the KScanOptSet, given in constructor */

   if( cfgName.isNull() || cfgName.isEmpty() )
      cfgName = "default";

   if( ! scanConfig->hasGroup( name ) )
   {
      kdDebug(29000) << "Group " << name << " does not exist in configuration !" << endl;
      ret = false;
   }
   else
   {
      scanConfig->setGroup( name );

      typedef TQMap<TQString, TQString> StringMap;

      StringMap strMap = scanConfig->entryMap( name );

      StringMap::Iterator it;
      for( it = strMap.begin(); it != strMap.end(); ++it )
      {
	 TQCString optName = it.key().latin1();
	 KScanOption optset( optName );

	 TQCString val = it.data().latin1();
	 kdDebug(29000) << "Reading for " << optName << " value " << val << endl;

	 optset.set( val );

	 backupOption( optset );
      }
   }
   delete( scanConfig );

   return( ret );
}

/* END */