summaryrefslogtreecommitdiffstats
path: root/kpf/src/ConfigDialogPage.cpp
blob: 12872a0ab4c0e99fdfb890abdcf2d0cd46555105 (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
/*
  KPF - Public fileserver for KDE

  Copyright 2001 Rik Hemsley (rikkus) <rik@kde.org>

  Permission is hereby granted, free of charge, to any person obtaining a copy
  of this software and associated documentation files (the "Software"), to
  deal in the Software without restriction, including without limitation the
  rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  sell copies of the Software, and to permit persons to whom the Software is
  furnished to do so, subject to the following conditions:

  The above copyright notice and this permission notice shall be included in
  all copies or substantial portions of the Software.

  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
  AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

#include <tqwhatsthis.h>
#include <tqlayout.h>
#include <tqspinbox.h>
#include <tqlabel.h>
#include <tqcheckbox.h>
#include <tqlineedit.h>

#include <klocale.h>
#include <kseparator.h>
#include <kfiledialog.h>

#include "Defines.h"
#include "ErrorMessageConfigDialog.h"
#include "ConfigDialogPage.h"
#include "WebServerManager.h"
#include "WebServer.h"
#include "Help.h"

#include <dnssd/servicebrowser.h>

namespace KPF
{
  ConfigDialogPage::ConfigDialogPage(WebServer * server, TQWidget * tqparent)
    : TQWidget                   (tqparent, "KPF::ConfigDialogPage"),
      server_                   (server),
      errorMessageConfigDialog_ (0L)
  {
    l_listenPort_       = new TQLabel(i18n("&Listen port:"),      this);
    l_bandwidthLimit_   = new TQLabel(i18n("&Bandwidth limit:"),  this);
//    l_connectionLimit_  = new TQLabel(i18n("Connection &limit"), this);

    sb_listenPort_      = new TQSpinBox(1,  65535, 1, this);
    sb_bandwidthLimit_  = new TQSpinBox(1,     999999, 1, this);
//    sb_connectionLimit_ = new TQSpinBox(1,     9999,   1, this);

    l_serverName_       = new TQLabel(i18n("&Server name:"),      this);
    le_serverName_      = new TQLineEdit(this);

    bool canPublish = DNSSD::ServiceBrowser::isAvailable() == DNSSD::ServiceBrowser::Working;
    l_serverName_->setEnabled(canPublish);
    le_serverName_->setEnabled(canPublish);
    
    cb_followSymlinks_  = new TQCheckBox(i18n("&Follow symbolic links"), this);

//    cb_customErrorMessages_ =
//      new TQCheckBox(i18n("Use custom error messages"), this);

//    pb_errorMessages_   = new TQPushButton(i18n("&Configure..."), this);

//    pb_errorMessages_->setEnabled(false);

    l_listenPort_       ->setBuddy(sb_listenPort_);
    l_bandwidthLimit_   ->setBuddy(sb_bandwidthLimit_);
    l_serverName_       ->setBuddy(le_serverName_);
//    l_connectionLimit_  ->setBuddy(sb_connectionLimit_);

    sb_listenPort_
      ->setValue(WebServerManager::instance()->nextFreePort());

    sb_bandwidthLimit_  ->setValue(Config::DefaultBandwidthLimit);
    sb_bandwidthLimit_  ->setSuffix(i18n(" kB/s"));
//    sb_connectionLimit_ ->setValue(Config::DefaultConnectionLimit);
    cb_followSymlinks_  ->setChecked(Config::DefaultFollowSymlinks);

    TQVBoxLayout * l0 = new TQVBoxLayout(this, 0, KDialog::spacingHint());

    TQGridLayout * l2 = new TQGridLayout(l0);

    l2->addWidget(l_listenPort_,       0, 0);
    l2->addWidget(sb_listenPort_,      0, 1);
    l2->addWidget(l_bandwidthLimit_,   1, 0);
    l2->addWidget(sb_bandwidthLimit_,  1, 1);
    l2->addWidget(l_serverName_,       2, 0);
    l2->addWidget(le_serverName_,      2, 1);
//    l2->addWidget(l_connectionLimit_,  2, 0);
//    l2->addWidget(sb_connectionLimit_, 2, 1);

    l0->addWidget(cb_followSymlinks_);

#if 0
    TQHBoxLayout * l3 = new TQHBoxLayout(l0);

    l3->addWidget(cb_customErrorMessages_);
    l3->addWidget(pb_errorMessages_);
#endif

    l0->addStretch(1);

#if 0
    connect
      (
       cb_customErrorMessages_,
       TQT_SIGNAL(toggled(bool)),
       TQT_SLOT(slotCustomErrorMessagesToggled(bool))
      );
#endif

#if 0
    connect
      (
       pb_errorMessages_,
       TQT_SIGNAL(clicked()),
       TQT_SLOT(slotConfigureErrorMessages())
      );
#endif

    TQString listenPortHelp =
      i18n
      (
       "<p>"
       "Specify the network `port' on which the server should"
       " listen for connections."
       "</p>"
      );

    TQString bandwidthLimitHelp =
      i18n
      (
       "<p>"
       "Specify the maximum amount of data (in kilobytes) that will be"
       " sent out per second."
       "</p>"
       "<p>"
       "This allows you to keep some bandwidth for yourself instead"
       " of allowing connections with kpf to hog your connection."
       "</p>"
      );

    TQString connectionLimitHelp =
      i18n
      (
       "<p>"
       "Specify the maximum number of connections allowed at"
       " any one time."
       "</p>"
      );

    TQString followSymlinksHelp =
      i18n
      (
       "<p>"
       "Allow serving of files which have a symbolic link in"
       " the path from / to the file, or are a symbolic link"
       " themselves."
       "</p>"
       "<p>"
       "<strong>Warning !</strong> This could be a security"
       " risk. Use only if you understand the issues involved."
       "</p>"
      );

    TQString errorMessagesHelp =
      i18n
      (
       "<p>"
       "Specify the text that will be sent upon an error,"
       " such as a request for a page that does not exist"
       " on this server."
       "</p>"
      );

    TQString serverNameHelp = KPF::HelpText::getServerNameHelp();
    TQWhatsThis::add(l_listenPort_,        listenPortHelp);
    TQWhatsThis::add(sb_listenPort_,       listenPortHelp);
    TQWhatsThis::add(l_bandwidthLimit_,    bandwidthLimitHelp);
    TQWhatsThis::add(sb_bandwidthLimit_,   bandwidthLimitHelp);
//    TQWhatsThis::add(l_connectionLimit_,   connectionLimitHelp);
//    TQWhatsThis::add(sb_connectionLimit_,  connectionLimitHelp);
    TQWhatsThis::add(cb_followSymlinks_,   followSymlinksHelp);
    TQWhatsThis::add(l_serverName_,        serverNameHelp);
    TQWhatsThis::add(le_serverName_,       serverNameHelp);
//    TQWhatsThis::add(pb_errorMessages_,    errorMessagesHelp);

    connect
      (
        sb_listenPort_,
        TQT_SIGNAL(valueChanged(int)),
        TQT_SLOT(slotListenPortChanged(int))
      );

    connect
      (
        sb_bandwidthLimit_,
        TQT_SIGNAL(valueChanged(int)),
        TQT_SLOT(slotBandwidthLimitChanged(int))
      );

    connect
      (
        cb_followSymlinks_,
        TQT_SIGNAL(toggled(bool)),
        TQT_SLOT(slotFollowSymlinksToggled(bool))
      );


    load();
  }

  ConfigDialogPage::~ConfigDialogPage()
  {
    // Empty.
  }

    void
  ConfigDialogPage::load()
  {
    sb_listenPort_          ->setValue(server_->listenPort());
    sb_bandwidthLimit_      ->setValue(server_->bandwidthLimit());
//    sb_connectionLimit_     ->setValue(server_->connectionLimit());
    cb_followSymlinks_      ->setChecked(server_->followSymlinks());
    le_serverName_          ->setText(server_->serverName());
//    cb_customErrorMessages_ ->setChecked(server_->customErrorMessages());
  }

    void
  ConfigDialogPage::save()
  {
    server_->setListenPort          (sb_listenPort_->value());
    server_->setBandwidthLimit      (sb_bandwidthLimit_->value());
//    server_->setConnectionLimit     (sb_connectionLimit_->value());
    server_->setFollowSymlinks      (cb_followSymlinks_->isChecked());
    server_->setCustomErrorMessages (cb_followSymlinks_->isChecked());
    server_->setServerName          (le_serverName_->text());
  }

    void
  ConfigDialogPage::slotCustomErrorMessagesToggled(bool)
  {
//    pb_errorMessages_->setEnabled(b);
  }

    void
  ConfigDialogPage::slotConfigureErrorMessages()
  {
    if (0 == errorMessageConfigDialog_)
      errorMessageConfigDialog_ = new ErrorMessageConfigDialog(server_, this);

    errorMessageConfigDialog_->show();
  }

    void
  ConfigDialogPage::slotListenPortChanged(int)
  {
    kpfDebug << "slotBandwidthLimitChanged" << endl;
    checkOkAndEmit();
  }

  void ConfigDialogPage::checkOk()
  {
    kpfDebug << "slotBandwidthLimitChanged" << endl;
    checkOkAndEmit();
  }

  void ConfigDialogPage::slotBandwidthLimitChanged(int)
  {
    kpfDebug << "slotBandwidthLimitChanged" << endl;
    checkOkAndEmit();
  }

  void ConfigDialogPage::slotFollowSymlinksToggled(bool)
  {
    kpfDebug << "slotBandwidthLimitChanged" << endl;
    checkOkAndEmit();
  }

  void ConfigDialogPage::checkOkAndEmit()
  {
    int newPort = sb_listenPort_->value();

    if (newPort <= 1024)
    {
      emit(ok(false));
      return;
    }

    TQPtrList<WebServer>
      serverList(WebServerManager::instance()->serverListLocal());

    for (TQPtrListIterator<WebServer> it(serverList); it.current(); ++it)
    {
      if (it.current() == server_)
        continue;

      if (it.current()->listenPort() == uint(newPort))
      {
        emit(ok(false));
        return;
      }
    }

    emit(ok(true));
  }
}
#include "ConfigDialogPage.moc"
// vim:ts=2:sw=2:tw=78:et