~ubuntu-branches/ubuntu/wily/tora/wily-proposed

« back to all changes in this revision

Viewing changes to tonewconnection.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Albin Tonnerre
  • Date: 2007-05-29 13:13:36 UTC
  • mfrom: (1.2.4 upstream)
  • Revision ID: james.westby@ubuntu.com-20070529131336-85ygaddivvmkd3xc
Tags: 1.3.21pre22-1ubuntu1
* Merge from Debian unstable. Remaining Ubuntu changes:
  - debian/rules: call dh_iconcache
  - Remove g++ build dependency
* Modify Maintainer value to match Debian-Maintainer-Field Spec

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*****
2
 
*
3
 
* TOra - An Oracle Toolkit for DBA's and developers
4
 
* Copyright (C) 2003-2005 Quest Software, Inc
5
 
* Portions Copyright (C) 2005 Other Contributors
6
 
7
 
* This program is free software; you can redistribute it and/or
8
 
* modify it under the terms of the GNU General Public License
9
 
* as published by the Free Software Foundation;  only version 2 of
10
 
* the License is valid for this program.
11
 
12
 
* This program is distributed in the hope that it will be useful,
13
 
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 
* GNU General Public License for more details.
16
 
17
 
* You should have received a copy of the GNU General Public License
18
 
* along with this program; if not, write to the Free Software
19
 
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
20
 
*
21
 
*      As a special exception, you have permission to link this program
22
 
*      with the Oracle Client libraries and distribute executables, as long
23
 
*      as you follow the requirements of the GNU GPL in regard to all of the
24
 
*      software in the executable aside from Oracle client libraries.
25
 
*
26
 
*      Specifically you are not permitted to link this program with the
27
 
*      Qt/UNIX, Qt/Windows or Qt Non Commercial products of TrollTech.
28
 
*      And you are not permitted to distribute binaries compiled against
29
 
*      these libraries without written consent from Quest Software, Inc.
30
 
*      Observe that this does not disallow linking to the Qt Free Edition.
31
 
*
32
 
*      You may link this product with any GPL'd Qt library such as Qt/Free
33
 
*
34
 
* All trademarks belong to their respective owners.
35
 
*
36
 
*****/
37
 
 
38
 
#include "utils.h"
39
 
 
40
 
#include "toconf.h"
41
 
#include "toconnection.h"
42
 
#include "tonewconnection.h"
43
 
#include "tomain.h"
44
 
#include "toresultview.h"
45
 
#include "totool.h"
46
 
 
47
 
#include <qbuttongroup.h>
48
 
#include <qcheckbox.h>
49
 
#include <qcombobox.h>
50
 
#include <qfile.h>
51
 
#include <qinputdialog.h>
52
 
#include <qlabel.h>
53
 
#include <qlayout.h>
54
 
#include <qlineedit.h>
55
 
#include <qlistview.h>
56
 
#include <qmessagebox.h>
57
 
#include <qpopupmenu.h>
58
 
#include <qpushbutton.h>
59
 
#include <qspinbox.h>
60
 
#include <qtooltip.h>
61
 
#include <qtooltip.h>
62
 
#include <qvariant.h>
63
 
#include <qwhatsthis.h>
64
 
 
65
 
#ifdef WIN32
66
 
#  include "windows/cregistry.h"
67
 
#endif
68
 
 
69
 
#ifdef TO_KDE_KACCELMANAGER
70
 
#  include <kaccelmanager.h>
71
 
#endif
72
 
 
73
 
#include "tonewconnection.moc"
74
 
#include "tonewconnectionui.moc"
75
 
 
76
 
toNewConnection::toNewConnection(QWidget* parent, const char* name, bool modal, WFlags fl)
77
 
        : toNewConnectionUI(parent, name, modal, fl),
78
 
        toHelpContext(QString::fromLatin1("newconnection.html"))
79
 
{
80
 
    toHelp::connectDialog(this);
81
 
 
82
 
    OptionGroup->setColumnLayout(0, Qt::Vertical );
83
 
    OptionGroup->layout()->setSpacing( 6 );
84
 
    OptionGroup->layout()->setMargin( 11 );
85
 
    OptionGroupLayout = new QVBoxLayout( OptionGroup->layout() );
86
 
    OptionGroupLayout->setAlignment( Qt::AlignTop );
87
 
#ifdef TO_KDE_KACCELMANAGER
88
 
    KAcceleratorManager::setNoAccel( OptionGroup );
89
 
#endif
90
 
 
91
 
    QPopupMenu *menu = new QPopupMenu(Previous);
92
 
    Database->insertItem(toTool::globalConfig(CONF_DATABASE, DEFAULT_DATABASE));
93
 
    Previous->addColumn(tr("Provider"));
94
 
    Previous->addColumn(tr("Host"));
95
 
    Previous->addColumn(tr("Database"));
96
 
    Previous->addColumn(tr("Username"));
97
 
    Previous->setSelectionMode(QListView::Single);
98
 
    Previous->setSizePolicy(QSizePolicy(QSizePolicy::Maximum, QSizePolicy::Expanding));
99
 
    menu->insertItem("Delete", TONEWCONNECTION_DELETE);
100
 
    connect(menu, SIGNAL(activated(int)), this, SLOT(menuCallback(int)));
101
 
    Previous->setDisplayMenu(menu);
102
 
    connect(Previous, SIGNAL(selectionChanged()), this, SLOT(historySelection()));
103
 
    connect(Previous, SIGNAL(doubleClicked(QListViewItem *)), this, SLOT(historyConnect()));
104
 
    std::list<QCString> lst = toConnectionProvider::providers();
105
 
    int sel = 0, cur = 0;
106
 
    QCString provider = toTool::globalConfig(CONF_PROVIDER, DEFAULT_PROVIDER).latin1();
107
 
    for (std::list<QCString>::iterator i = lst.begin();i != lst.end();i++)
108
 
    {
109
 
        Provider->insertItem(QString::fromLatin1(*i));
110
 
        if (*i == provider)
111
 
            sel = cur;
112
 
        cur++;
113
 
    }
114
 
    if (cur == 0)
115
 
    {
116
 
        TOMessageBox::information(this,
117
 
                                  tr("No connection provider"),
118
 
                                  tr("No available connection provider, plugins probably missing"));
119
 
        reject();
120
 
        return ;
121
 
    }
122
 
    Provider->setCurrentItem(sel);
123
 
    changeProvider();
124
 
    processOptions(toTool::globalConfig(CONF_OPTIONS, DEFAULT_OPTIONS));
125
 
 
126
 
    QString host = toTool::globalConfig(CONF_HOST, DEFAULT_HOST);
127
 
 
128
 
    int portix = host.find(":");
129
 
    if (portix >= 0)
130
 
    {
131
 
        Host->lineEdit()->setText(host.mid(0, portix));
132
 
        Port->setValue(host.mid(portix + 1).toInt());
133
 
    }
134
 
    else
135
 
        Host->lineEdit()->setText(host);
136
 
 
137
 
    Username->setText(toTool::globalConfig(CONF_USER, DEFAULT_USER));
138
 
    Username->setFocus();
139
 
 
140
 
    bool pass = toTool::globalConfig(CONF_SAVE_PWD, DEFAULT_SAVE_PWD).isEmpty();
141
 
    if (pass)
142
 
        Password->setText(QString::fromLatin1(DEFAULT_PASSWORD));
143
 
    else
144
 
        Password->setText(toUnobfuscate(toTool::globalConfig(CONF_PASSWORD, DEFAULT_PASSWORD)));
145
 
 
146
 
    QString defdb = toTool::globalConfig(CONF_DATABASE, DEFAULT_DATABASE);
147
 
    Database->setEditable(true);
148
 
 
149
 
    {
150
 
        int maxHist = toTool::globalConfig(CONF_CONNECT_CURRENT, 0).toInt();
151
 
        Previous->setSorting( -1);
152
 
        QListViewItem *last = NULL;
153
 
        for (int i = 0;i < maxHist;i++)
154
 
        {
155
 
            QCString path = CONF_CONNECT_HISTORY;
156
 
            path += ":";
157
 
            path += QString::number(i).latin1();
158
 
            QCString tmp = path;
159
 
            tmp += CONF_USER;
160
 
            QString user = toTool::globalConfig(tmp, "");
161
 
 
162
 
            tmp = path;
163
 
            tmp += CONF_PASSWORD;
164
 
            QString passstr = (pass ? QString::fromLatin1(DEFAULT_PASSWORD) :
165
 
                               (toUnobfuscate(toTool::globalConfig(tmp, DEFAULT_PASSWORD))));
166
 
 
167
 
            tmp = path;
168
 
            tmp += CONF_HOST;
169
 
            QString host = toTool::globalConfig(tmp, DEFAULT_HOST);
170
 
 
171
 
            tmp = path;
172
 
            tmp += CONF_DATABASE;
173
 
            QString database = toTool::globalConfig(tmp, DEFAULT_DATABASE);
174
 
 
175
 
            tmp = path;
176
 
            tmp += CONF_PROVIDER;
177
 
            QString provider = toTool::globalConfig(tmp, DEFAULT_PROVIDER);
178
 
 
179
 
            tmp = path;
180
 
            tmp += CONF_OPTIONS;
181
 
            QString options = toTool::globalConfig(tmp, DEFAULT_OPTIONS);
182
 
 
183
 
            last = new QListViewItem(Previous, last, provider, host, database, user, passstr, options);
184
 
        }
185
 
    }
186
 
    adjustSize();
187
 
}
188
 
 
189
 
toNewConnection::~toNewConnection()
190
 
{}
191
 
 
192
 
void toNewConnection::changeProvider(void)
193
 
{
194
 
    try
195
 
    {
196
 
        std::list<QString> hosts = toConnectionProvider::hosts(Provider->currentText().latin1());
197
 
        QString current = Host->currentText();
198
 
 
199
 
        bool sqlNet = false;
200
 
 
201
 
        Host->clear();
202
 
        DefaultPort = 0;
203
 
        for (std::list<QString>::iterator i = hosts.begin();i != hosts.end();i++)
204
 
        {
205
 
            if ((*i).isEmpty())
206
 
                sqlNet = true;
207
 
            else if ((*i).startsWith(":"))
208
 
            {
209
 
                DefaultPort = (*i).mid(1).toInt();
210
 
            }
211
 
            else
212
 
                Host->insertItem(*i);
213
 
        }
214
 
        Port->setValue(DefaultPort);
215
 
        if (sqlNet)
216
 
        {
217
 
            HostLabel->hide();
218
 
            Host->hide();
219
 
            PortLabel->hide();
220
 
            Port->hide();
221
 
        }
222
 
        else
223
 
        {
224
 
            HostLabel->show();
225
 
            Host->show();
226
 
            PortLabel->show();
227
 
            Port->show();
228
 
        }
229
 
        Host->lineEdit()->setText(current);
230
 
 
231
 
        for (std::list<QWidget *>::iterator k = OptionWidgets.begin();k != OptionWidgets.end();k++)
232
 
        {
233
 
            if ((*k)->isA("QCheckBox"))
234
 
            {
235
 
                QCheckBox *box = (QCheckBox *)(*k);
236
 
                Options[box->text()] = box->isChecked();
237
 
            }
238
 
            delete *k;
239
 
        }
240
 
        OptionWidgets.clear();
241
 
 
242
 
        std::list<QString> options = toConnectionProvider::options(Provider->currentText().latin1());
243
 
        for (std::list<QString>::iterator j = options.begin();j != options.end();j++)
244
 
        {
245
 
            if ((*j) == "-")
246
 
            {
247
 
                QFrame *frame = new QFrame(OptionGroup);
248
 
                frame->setFrameShape( QFrame::HLine );
249
 
                frame->setFrameShadow( QFrame::Sunken );
250
 
                OptionGroupLayout->addWidget(frame);
251
 
                frame->show();
252
 
                OptionWidgets.insert(OptionWidgets.end(), frame);
253
 
            }
254
 
            else
255
 
            {
256
 
                QString option = *j;
257
 
                bool defOn = false;
258
 
                if (option.startsWith("*"))
259
 
                {
260
 
                    defOn = true;
261
 
                    option = option.mid(1);
262
 
                }
263
 
 
264
 
                QCheckBox *ow = new QCheckBox(OptionGroup);
265
 
                ow->setText(option);
266
 
                if (Options.find(option) != Options.end())
267
 
                    ow->setChecked((*(Options.find(option))).second);
268
 
                else
269
 
                    ow->setChecked(defOn);
270
 
                OptionGroupLayout->addWidget(ow);
271
 
                ow->show();
272
 
                OptionWidgets.insert(OptionWidgets.end(), ow);
273
 
            }
274
 
        }
275
 
        if ( options.empty() )
276
 
            OptionGroup->hide();
277
 
        else
278
 
            OptionGroup->show();
279
 
 
280
 
    }
281
 
    catch (const QString &str)
282
 
    {
283
 
        Host->clear();
284
 
        toStatusMessage(str);
285
 
    }
286
 
}
287
 
 
288
 
void toNewConnection::changeHost(void)
289
 
{
290
 
    try
291
 
    {
292
 
        if (!Host->isHidden())
293
 
        {
294
 
            QString host;
295
 
            host = Host->currentText();
296
 
            std::list<QString> databases = toConnectionProvider::databases(Provider->currentText().latin1(),
297
 
                                           host,
298
 
                                           Username->text(),
299
 
                                           Password->text());
300
 
            QString current = Database->currentText();
301
 
 
302
 
            Database->clear();
303
 
            for (std::list<QString>::iterator i = databases.begin();i != databases.end();i++)
304
 
                Database->insertItem(*i);
305
 
            Database->lineEdit()->setText(current);
306
 
        }
307
 
    }
308
 
    catch (const QString &str)
309
 
    {
310
 
        Database->clear();
311
 
        toStatusMessage(str);
312
 
    }
313
 
}
314
 
 
315
 
void toNewConnection::processOptions(const QString &str)
316
 
{
317
 
    QStringList options = QStringList::split(",", str);
318
 
    std::map<QString, bool> values;
319
 
    for (unsigned int i = 0;i < options.count();i++)
320
 
    {
321
 
        QString val = options[i];
322
 
        if (val.startsWith("*"))
323
 
            values[val.mid(1)] = true;
324
 
        else
325
 
            values[val] = false;
326
 
    }
327
 
    for (std::list<QWidget *>::iterator k = OptionWidgets.begin();k != OptionWidgets.end();k++)
328
 
    {
329
 
        if ((*k)->isA("QCheckBox"))
330
 
        {
331
 
            QCheckBox *box = (QCheckBox *)(*k);
332
 
            if (values.find(box->text()) != values.end())
333
 
                box->setChecked(values[box->text()]);
334
 
        }
335
 
    }
336
 
}
337
 
 
338
 
toConnection *toNewConnection::makeConnection(void)
339
 
{
340
 
    try
341
 
    {
342
 
        toTool::globalSetConfig(CONF_PROVIDER, Provider->currentText());
343
 
        toTool::globalSetConfig(CONF_USER, Username->text());
344
 
        QString pass;
345
 
        QString host;
346
 
        if (!Host->isHidden())
347
 
            host = Host->currentText();
348
 
 
349
 
        QString optionstring;
350
 
        std::set
351
 
            <QString> options;
352
 
 
353
 
        for (std::list<QWidget *>::iterator k = OptionWidgets.begin();k != OptionWidgets.end();k++)
354
 
        {
355
 
            if ((*k)->isA("QCheckBox"))
356
 
            {
357
 
                if (!optionstring.isEmpty())
358
 
                    optionstring += ",";
359
 
                QCheckBox *box = (QCheckBox *)(*k);
360
 
                if (box->isChecked())
361
 
                {
362
 
                    optionstring += "*";
363
 
                    options.insert(box->text());
364
 
                }
365
 
                optionstring += box->text();
366
 
            }
367
 
        }
368
 
        toTool::globalSetConfig(CONF_OPTIONS, optionstring);
369
 
 
370
 
        std::list<QString> con = toMainWidget()->connections();
371
 
        for (std::list<QString>::iterator i = con.begin();i != con.end();i++)
372
 
        {
373
 
            try
374
 
            {
375
 
                toConnection &conn = toMainWidget()->connection(*i);
376
 
                if (conn.user() == Username->text() &&
377
 
                        conn.provider() == Provider->currentText().latin1() &&
378
 
                        conn.host() == host &&
379
 
                        conn.database() == Database->currentText())
380
 
                    return &conn;
381
 
            }
382
 
            catch (...)
383
 
            {}
384
 
        }
385
 
        if (Port->value() != 0 && Port->value() != DefaultPort)
386
 
            host += ":" + QString::number(Port->value());
387
 
 
388
 
        toTool::globalSetConfig(CONF_HOST, host);
389
 
 
390
 
        toConnection *retCon = new toConnection(Provider->currentText().latin1(),
391
 
                                                Username->text(),
392
 
                                                Password->text(),
393
 
                                                host,
394
 
                                                Database->currentText(),
395
 
                                                options);
396
 
        {
397
 
            for (QListViewItem *item = Previous->firstChild();item;item = item->nextSibling())
398
 
            {
399
 
                if (item->text(0) == Provider->currentText() &&
400
 
                        ((item->text(1) == host) || (item->text(1).isEmpty() && host.isEmpty())) &&
401
 
                        item->text(2) == Database->currentText() &&
402
 
                        item->text(3) == Username->text())
403
 
                {
404
 
                    delete item;
405
 
                    break;
406
 
                }
407
 
            }
408
 
        }
409
 
 
410
 
        if (!toTool::globalConfig(CONF_SAVE_PWD, DEFAULT_SAVE_PWD).isEmpty())
411
 
            pass = retCon->password();
412
 
        else
413
 
            pass = DEFAULT_PASSWORD;
414
 
        toTool::globalSetConfig(CONF_PASSWORD, toObfuscate(pass));
415
 
        toTool::globalSetConfig(CONF_DATABASE, Database->currentText());
416
 
 
417
 
        new QListViewItem(Previous, NULL,
418
 
                          Provider->currentText(),
419
 
                          host,
420
 
                          Database->currentText(),
421
 
                          Username->text(),
422
 
                          retCon->password(),
423
 
                          optionstring);
424
 
        historySave();
425
 
        return retCon;
426
 
    }
427
 
    catch (const QString &exc)
428
 
    {
429
 
        QString str = tr("Unable to connect to the database.\n");
430
 
        str.append(exc);
431
 
        TOMessageBox::information(this,
432
 
                                  tr("Unable to connect to the database"),
433
 
                                  str);
434
 
        return NULL;
435
 
    }
436
 
}
437
 
 
438
 
void toNewConnection::historySave(void)
439
 
{
440
 
    int siz = toTool::globalConfig(CONF_CONNECT_SIZE, DEFAULT_CONNECT_SIZE).toInt();
441
 
    int i = 0;
442
 
    int j = 0;
443
 
 
444
 
    for (QListViewItem *item = Previous->firstChild();i < siz;item = (item ? item = item->nextSibling() : 0))
445
 
    {
446
 
        QCString path = CONF_CONNECT_HISTORY;
447
 
        path += ":";
448
 
        path += QString::number(i).latin1();
449
 
 
450
 
        QCString tmp = path;
451
 
        tmp += CONF_PROVIDER;
452
 
        if (i < siz && item)
453
 
            toTool::globalSetConfig(tmp, item->text(0));
454
 
        else
455
 
            toTool::globalEraseConfig(tmp);
456
 
 
457
 
        tmp = path;
458
 
        tmp += CONF_HOST;
459
 
        if (i < siz && item)
460
 
            toTool::globalSetConfig(tmp, item->text(1));
461
 
        else
462
 
            toTool::globalEraseConfig(tmp);
463
 
 
464
 
        tmp = path;
465
 
        tmp += CONF_DATABASE;
466
 
        if (i < siz && item)
467
 
            toTool::globalSetConfig(tmp, item->text(2));
468
 
        else
469
 
            toTool::globalEraseConfig(tmp);
470
 
 
471
 
        tmp = path;
472
 
        tmp += CONF_USER;
473
 
        if (i < siz && item)
474
 
            toTool::globalSetConfig(tmp, item->text(3));
475
 
        else
476
 
            toTool::globalEraseConfig(tmp);
477
 
 
478
 
        tmp = path;
479
 
        tmp += CONF_PASSWORD;
480
 
        if (i < siz && item)
481
 
            toTool::globalSetConfig(tmp, toObfuscate(item->text(4)));
482
 
        else
483
 
            toTool::globalEraseConfig(tmp);
484
 
 
485
 
        tmp = path;
486
 
        tmp += CONF_OPTIONS;
487
 
        if (i < siz && item)
488
 
            toTool::globalSetConfig(tmp, item->text(5));
489
 
        else
490
 
            toTool::globalEraseConfig(tmp);
491
 
 
492
 
        i++;
493
 
        if (i < siz && item)
494
 
            j++;
495
 
    }
496
 
    toTool::globalSetConfig(CONF_CONNECT_CURRENT, QString::number(j));
497
 
    toTool::saveConfig();
498
 
}
499
 
 
500
 
void toNewConnection::historySelection(void)
501
 
{
502
 
    QListViewItem *item = Previous->selectedItem();
503
 
    if (item)
504
 
    {
505
 
        for (int i = 0;i < Provider->count();i++)
506
 
            if (Provider->text(i) == item->text(0))
507
 
            {
508
 
                Provider->setCurrentItem(i);
509
 
                break;
510
 
            }
511
 
        changeProvider();
512
 
 
513
 
        QString host = item->text(1);
514
 
        int portix = host.find(":");
515
 
        if (portix >= 0)
516
 
        {
517
 
            Host->lineEdit()->setText(host.mid(0, portix));
518
 
            Port->setValue(host.mid(portix + 1).toInt());
519
 
        }
520
 
        else
521
 
        {
522
 
            Host->lineEdit()->setText(host);
523
 
            Port->setValue(DefaultPort);
524
 
        }
525
 
 
526
 
        processOptions(item->text(5));
527
 
 
528
 
        Database->lineEdit()->setText(item->text(2));
529
 
        Username->setText(item->text(3));
530
 
        if (item->text(4) != DEFAULT_PASSWORD)
531
 
            Password->setText(item->text(4));
532
 
    }
533
 
}
534
 
 
535
 
void toNewConnection::historyConnect(void)
536
 
{
537
 
    bool ok = true;
538
 
    if (toTool::globalConfig(CONF_SAVE_PWD, DEFAULT_SAVE_PWD).isEmpty())
539
 
    {
540
 
        ok = false;
541
 
        QString name = QInputDialog::getText(tr("Enter password"),
542
 
                                             tr("Enter password to use for connection."),
543
 
                                             QLineEdit::Password, QString::null, &ok, this);
544
 
        if (ok)
545
 
            Password->setText(name);
546
 
    }
547
 
    if (ok)
548
 
        accept();
549
 
}
550
 
void toNewConnection::menuCallback(int cmd)
551
 
{
552
 
    switch (cmd)
553
 
    {
554
 
    case TONEWCONNECTION_DELETE:
555
 
        {
556
 
            historyDelete();
557
 
            break;
558
 
        }
559
 
    }
560
 
}
561
 
 
562
 
void toNewConnection::historyDelete()
563
 
{
564
 
    QListViewItem *item = Previous->selectedItem();
565
 
    if (item)
566
 
    {
567
 
        Previous->takeItem(item);
568
 
        delete item;
569
 
        historySave();
570
 
    }
571
 
}