summaryrefslogtreecommitdiffstats
path: root/filesharing/advanced/kcm_sambaconf/sambashare.cpp
blob: ad2441456f4c7b9e683dd7085e11723a568ddedf (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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
/***************************************************************************
                          sambashare.cpp  -  description
                            -------------------
    begin                : Mon Jun 12 2002
    copyright            : (C) 2002 by Jan Sch�fer
    email                : [email protected]
***************************************************************************/

/******************************************************************************
*                                                                            *
*  This file is part of KSambaPlugin.                                        *
*                                                                            *
*  KSambaPlugin 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.                                       *
*                                                                            *
*  KSambaPlugin 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 General Public License for more details.                              *
*                                                                            *
*  You should have received a copy of the GNU General Public License         *
*  along with KSambaPlugin; if not, write to the Free Software                     *
*  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02111-1307 USA  *
*                                                                            *
******************************************************************************/

#include <kdebug.h>
#include <qstringlist.h>

#include "sambafile.h"
#include "sambashare.h"
#include "common.h"

SambaShare::SambaShare(SambaConfigFile* sambaFile)
  : QDict<QString>(10,false)
{
  _sambaFile = sambaFile;
  setName("defaults");
  setAutoDelete(true);
}

SambaShare::SambaShare(const QString & name, SambaConfigFile* sambaFile)
  : QDict<QString>(10,false)
{
  _sambaFile = sambaFile;
  setName(name);
  setAutoDelete(true);
}

const QString& SambaShare::getName() const
{
  return _name;
}

bool SambaShare::setName(const QString & name, bool testWetherExists)
{

  if ( testWetherExists &&
      _sambaFile->find(name) &&
      _sambaFile->find(name) != this)
    return false;

  _name = name;
return true;
}

bool SambaShare::optionSupported(const QString & name) 
{
  QString defaultValue = _sambaFile->getDefaultValue(name);
//  kdDebug(5009) << name << " = " << defaultValue << " null : " << defaultValue.isNull() << endl;
  return ! defaultValue.isNull();
}

/**
* Returns the value of the given parameter
* if no value is set yet the default value
* will be returned.
**/
QString SambaShare::getValue(const QString & name, bool globalValue, bool defaultValue)
{
  QString synonym = getSynonym(name);

  QString* str = find(synonym);
  QString ret;

  if (str) {
    ret = *str;
  }     
  else
  if (globalValue)
    ret = getGlobalValue(synonym,defaultValue);
  else
  if (defaultValue)
    ret = getDefaultValue(synonym);


  if (name=="writable" || name=="write ok" || name=="writeable")
    ret = textFromBool( ! boolFromText(ret) );

  return ret;
}

bool SambaShare::getBoolValue(const QString & name, bool globalValue, bool defaultValue)
{
  return boolFromText(getValue(name,globalValue,defaultValue));
}


QString SambaShare::getGlobalValue(const QString & name, bool defaultValue)
{
  if (!_sambaFile)
    return getValue(name,false,defaultValue);

  SambaShare* globals = _sambaFile->find("global");

  QString s = globals->getValue(name,false,defaultValue);

  return s;
}


/**
* Returns the default synonym for the given parameter
* if no synonym exists the original parameter in lower
* case is returned
**/
QString SambaShare::getSynonym(const QString & name) const
{

  // idmap config is an exception and shouldn't be set to lower
  QString sname = name.left(12).stripWhiteSpace();
  if ( sname == "idmap config" ) return name;

  QString lname = name.lower().stripWhiteSpace();

  if (lname == "browsable") return "browseable";
  if (lname == "allow hosts") return "hosts allow";
  if (lname == "auto services") return "preload";
  if (lname == "casesignames") return "case sensitive";
  if (lname == "create mode") return "create mask";
  if (lname == "debuglevel") return "log level";
  if (lname == "default") return "default service";
  if (lname == "deny hosts") return "hosts deny";
  if (lname == "directory") return "path";
  if (lname == "directory mode") return "directory mask";
  if (lname == "exec") return "preexec";
  if (lname == "group") return "force group";
  if (lname == "lock dir") return "lock directory";
  if (lname == "min passwd length") return "min password length";
  if (lname == "only guest") return "guest only";
  if (lname == "prefered master") return "preferred master";
  if (lname == "print ok") return "printable";
  if (lname == "printcap") return "printcap name";
  if (lname == "printer") return "printer name";
  if (lname == "protocol") return "max protocol";
  if (lname == "public") return "guest ok";
  if (lname == "writable") return "read only";
  if (lname == "write ok") return "read only";
  if (lname == "read only") return "read only";
  if (lname == "root") return "root directory";
  if (lname == "root") return "root dir";
  if (lname == "timestamp logs") return "debug timestamp";
  if (lname == "user") return "username";
  if (lname == "users") return "username";
  if (lname == "idmap uid") return "winbind uid";
  if (lname == "idmap gid") return "winbind gid";
  if (lname == "vfs object") return "vfs objects";


  return lname;
}

void SambaShare::setValue(const QString & name, const QString & value, bool globalValue, bool defaultValue)
{
  QString synonym = getSynonym(name);

  QString newValue = value;

  if (newValue.isNull())
    newValue = "";
  
  if (getName().lower() == "global")
    globalValue = false;

  if (name=="writable" || name=="write ok" || name=="writeable")
  {
    synonym = "read only";
    newValue = textFromBool(!boolFromText(value));
  }

  QString global = "";

  if (globalValue && !hasComments(synonym))
  {
    global = getGlobalValue(synonym, false);

    if ( newValue.lower() == global.lower() )
    {
      remove(synonym);
      _optionList.remove(synonym);
      return;
    }
  }

  // If the option has a comment we don't remove
  // it if the value is equal to the default value.
  // That's because the author of the option has thought about it.
  if (defaultValue && global.isEmpty() && !hasComments(synonym))
  {
    if ( newValue.stripWhiteSpace().lower() == getDefaultValue(synonym).stripWhiteSpace().lower() )
    {
      kdDebug(5009) << getName() << " global: " << global << " remove " << synonym << endl;
      remove(synonym);
      _optionList.remove(synonym);
      return;
    } 
    
  }

  if (!find(synonym))
  {
    _optionList.append(synonym);
  }
    
  replace(synonym,new QString(newValue));
}

void SambaShare::setValue(const QString & name, bool value, bool globalValue, bool defaultValue)
{
  setValue(name,textFromBool(value),globalValue, defaultValue);
}

void SambaShare::setValue(const QString & name, int value, bool globalValue, bool defaultValue)
{
  setValue(name,QString::number(value),globalValue, defaultValue);
}

/**
* Returns the default value of the parameter
**/
QString SambaShare::getDefaultValue(const QString & name)
{
  QString defaultValue = _sambaFile->getDefaultValue(name);
  if (defaultValue.isNull())
    defaultValue = "";

  return defaultValue;
}

bool SambaShare::getDefaultBoolValue(const QString & name)
{

  return boolFromText(getDefaultValue(name));
}

/**
* Sets the comments for the passed option
**/
void SambaShare::setComments(const QString & name, const QStringList & commentList)
{
  // Only add inempty lists
  if (commentList.empty())
    return;

  QString synonym = getSynonym(name);

  _commentList.replace(name,new QStringList(commentList));
}

/**
* Returns the comments of the passed option
**/
QStringList SambaShare::getComments(const QString & name)
{
  QStringList* list = _commentList.find(getSynonym(name));

  if (list)
    return QStringList(*list);
  else
    return QStringList();
}


bool SambaShare::hasComments(const QString & name)
{
  return 0L != _commentList.find(getSynonym(name));
}

/**
* Returns the comments of the share
* e.g. the text above the [...] section
**/
QStringList SambaShare::getComments()
{
  return _comments;
}

/**
* Sets the comments for the share
* e.g. the text above the [...] section
**/
void SambaShare::setComments(const QStringList & commentList)
{
  _comments = commentList;
}

QStringList SambaShare::getOptionList()
{
  return _optionList;
}

/**
* Returns true if this share is a printer
**/
bool SambaShare::isPrinter()
{
  QString* str = find("printable");

  if (!str)
      str = find("print ok");

  return str!=0;
}

/**
* Returns true if the share name is
* global, printers or homes
**/
bool SambaShare::isSpecialSection()
{
  if ( _name.lower() == "global" ||
      _name.lower() == "printers" ||
      _name.lower() == "homes" )
    return true;
  else
    return false;
}