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

« back to all changes in this revision

Viewing changes to tochartmanager.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 "tobarchart.h"
41
 
#include "toconf.h"
42
 
#include "toconnection.h"
43
 
#include "tolinechart.h"
44
 
#include "tomain.h"
45
 
#include "toresultview.h"
46
 
#include "tosmtp.h"
47
 
#include "totool.h"
48
 
 
49
 
#include <ctype.h>
50
 
 
51
 
#ifdef TO_KDE
52
 
#  include <kmenubar.h>
53
 
#endif
54
 
 
55
 
#include <qcheckbox.h>
56
 
#include <qcombobox.h>
57
 
#include <qlabel.h>
58
 
#include <qlayout.h>
59
 
#include <qlineedit.h>
60
 
#include <qmenubar.h>
61
 
#include <qmessagebox.h>
62
 
#include <qpopupmenu.h>
63
 
#include <qtoolbar.h>
64
 
#include <qtoolbutton.h>
65
 
#include <qvalidator.h>
66
 
#include <qworkspace.h>
67
 
 
68
 
#include "tochartalarmui.moc"
69
 
#include "tochartmanager.moc"
70
 
#include "tochartsetupui.moc"
71
 
 
72
 
#include "icons/chart.xpm"
73
 
#include "icons/fileopen.xpm"
74
 
#include "icons/refresh.xpm"
75
 
 
76
 
class toChartTool : public toTool
77
 
{
78
 
protected:
79
 
    toChartHandler *Handler;
80
 
    toChartManager *Window;
81
 
public:
82
 
    toChartTool()
83
 
            : toTool(930, "Chart Manager")
84
 
    {
85
 
        Window = NULL;
86
 
    }
87
 
    virtual QWidget *toolWindow(QWidget *, toConnection &)
88
 
    {
89
 
        if (!Handler)
90
 
            return NULL;
91
 
        if (!Window)
92
 
        {
93
 
            Window = new toChartManager(toMainWidget()->workspace());
94
 
            Window->setCaption(qApp->translate("toChartTool", "Chart Manager"));
95
 
            Window->setIcon(QPixmap(const_cast<const char**>(chart_xpm)));
96
 
        }
97
 
        Window->refresh();
98
 
        Window->show();
99
 
        Window->raise();
100
 
        Window->setFocus();
101
 
        return Window;
102
 
    }
103
 
    virtual void customSetup(int toolid)
104
 
    {
105
 
        toMainWidget()->editMenu()->insertItem(QPixmap(const_cast<const char**>(chart_xpm)),
106
 
                                               qApp->translate("toChartTool", "Chart Manager..."), toolid);
107
 
 
108
 
        Handler = new toChartHandler();
109
 
    }
110
 
    void closeWindow(void)
111
 
    {
112
 
        Window = NULL;
113
 
    }
114
 
    void closeHandler(void)
115
 
    {
116
 
        Handler = NULL;
117
 
    }
118
 
 
119
 
    toChartHandler *handler()
120
 
    {
121
 
        return Handler;
122
 
    }
123
 
    toChartManager *manager()
124
 
    {
125
 
        return Window;
126
 
    }
127
 
 
128
 
    virtual bool canHandle(toConnection &)
129
 
    {
130
 
        return true;
131
 
    }
132
 
};
133
 
 
134
 
static toChartTool ChartTool;
135
 
 
136
 
class toChartAlarm : public toChartAlarmUI
137
 
{
138
 
public:
139
 
    toChartAlarm(QWidget *parent = 0, const char *name = 0, bool modal = FALSE, WFlags fl = 0)
140
 
            : toChartAlarmUI(parent, name, modal, fl)
141
 
    { }
142
 
    virtual void changeValue(int val)
143
 
    {
144
 
        if (val == 1)
145
 
            ExtraLabel->setText(qApp->translate("toChartAlarm", "Email"));
146
 
        else
147
 
            ExtraLabel->setText(QString::null);
148
 
 
149
 
        Extra->setEnabled(val == 1);
150
 
    }
151
 
};
152
 
 
153
 
class toChartSetup : public toChartSetupUI
154
 
{
155
 
    toLineChart *Chart;
156
 
public:
157
 
    toChartSetup(toLineChart *chart, QWidget* parent, const char* name, bool modal)
158
 
            : toChartSetupUI(parent, name, modal)
159
 
    {
160
 
        Chart = chart;
161
 
    }
162
 
 
163
 
    virtual void browseFile()
164
 
    {
165
 
        QString str = toOpenFilename(Filename->text(), QString::fromLatin1("*.csv"), this);
166
 
        if (!str.isEmpty())
167
 
            Filename->setText(str);
168
 
    }
169
 
    virtual QString modifyAlarm(const QString &str, bool &persistent)
170
 
    {
171
 
        toChartManager::chartAlarm alarm(str, persistent);
172
 
        toChartAlarm diag(this, NULL, true);
173
 
        diag.Operation->setCurrentItem((int)alarm.Operation);
174
 
        diag.Comparison->setCurrentItem((int)alarm.Comparison);
175
 
        diag.Action->setCurrentItem((int)alarm.Action);
176
 
        diag.Value->setText(QString::number(alarm.Value));
177
 
        diag.changeValue((int)alarm.Action);
178
 
        diag.Value->setValidator(new QDoubleValidator(diag.Value));
179
 
        diag.Extra->setText(alarm.Extra);
180
 
        std::list<int>::iterator sel = alarm.Columns.begin();
181
 
        diag.Charts->addColumn(qApp->translate("toChartSetup", "Charts"));
182
 
        diag.Charts->setSelectionMode(QListView::Multi);
183
 
        QListViewItem *item = NULL;
184
 
        std::list<QString>::iterator lab = Chart->labels().begin();
185
 
        for (int i = 0;lab != Chart->labels().end();i++, lab++)
186
 
        {
187
 
            item = new QListViewItem(diag.Charts, item, *lab);
188
 
            if (sel != alarm.Columns.end() && *sel == i)
189
 
            {
190
 
                item->setSelected(true);
191
 
                sel++;
192
 
            }
193
 
        }
194
 
        diag.Persistent->setChecked(persistent);
195
 
        if (diag.exec())
196
 
        {
197
 
            persistent = diag.Persistent->isChecked();
198
 
            int i = 0;
199
 
            alarm.Columns.clear();
200
 
            for (QListViewItem *item = diag.Charts->firstChild();item;item = item->nextSibling())
201
 
            {
202
 
                if (item->isSelected())
203
 
                    alarm.Columns.insert(alarm.Columns.end(), i);
204
 
                i++;
205
 
            }
206
 
            alarm.Action = (toChartManager::action)diag.Action->currentItem();
207
 
            alarm.Operation = (toChartManager::chartAlarm::operation)diag.Operation->currentItem();
208
 
            alarm.Comparison = (toChartManager::chartAlarm::comparison)diag.Comparison->currentItem();
209
 
            alarm.Value = diag.Value->text().toDouble();
210
 
            alarm.Extra = diag.Extra->text();
211
 
            return alarm.toString();
212
 
        }
213
 
        return QString::null;
214
 
    }
215
 
    virtual void addAlarm()
216
 
    {
217
 
        toChartManager::chartAlarm alarm;
218
 
        QString str = modifyAlarm(alarm.toString(), alarm.Persistent);
219
 
        if (!str.isEmpty())
220
 
            new QListViewItem(Alarms, str, alarm.Persistent ? qApp->translate("toChartSetup", "Persistent") : qApp->translate("toChartSetup", "Temporary"));
221
 
    }
222
 
    virtual void modifyAlarm()
223
 
    {
224
 
        QListViewItem *item = Alarms->selectedItem();
225
 
        if (item)
226
 
        {
227
 
            bool pers = item->text(1) == qApp->translate("toChartSetup", "Persistent");
228
 
            QString str = modifyAlarm(item->text(0), pers);
229
 
            if (!str.isEmpty())
230
 
            {
231
 
                item->setText(0, str);
232
 
                item->setText(1, pers ? qApp->translate("toChartSetup", "Persistent") : qApp->translate("toChartSetup", "Temporary"));
233
 
            }
234
 
        }
235
 
    }
236
 
    virtual void removeAlarm()
237
 
    {
238
 
        delete Alarms->selectedItem();
239
 
    }
240
 
};
241
 
 
242
 
toChartReceiver::toChartReceiver(toChartHandler *parent, toLineChart *chart)
243
 
        : QObject(parent)
244
 
{
245
 
    Parent = parent;
246
 
    Chart = chart;
247
 
    Result = NULL;
248
 
    connect(Chart, SIGNAL(valueAdded(std::list<double> &, const QString &)),
249
 
            this, SLOT(valueAdded(std::list<double> &, const QString &)));
250
 
}
251
 
 
252
 
toResult *toChartReceiver::result(void)
253
 
{
254
 
    if (!Result)
255
 
        Result = dynamic_cast<toResult *>(Chart);
256
 
    return Result;
257
 
}
258
 
 
259
 
QString toChartReceiver::name(void)
260
 
{
261
 
    if (!Result || Result->sqlName().isEmpty())
262
 
        return QString::null;
263
 
    try
264
 
    {
265
 
        LastName = Result->connection().description(false) + QString::fromLatin1(":") + QString::fromLatin1(Result->sqlName());
266
 
    }
267
 
    catch (...)
268
 
    {}
269
 
    return LastName;
270
 
}
271
 
 
272
 
void toChartReceiver::valueAdded(std::list<double> &value, const QString &xValues)
273
 
{
274
 
    Parent->valueAdded(Chart, name(), value, xValues);
275
 
}
276
 
 
277
 
toChartManager::toChartManager(QWidget *main)
278
 
        : QVBox(main), toHelpContext(QString::fromLatin1("chartmanager.html"))
279
 
{
280
 
    QToolBar *toolbar = toAllocBar(this, tr("Chart Manager"));
281
 
 
282
 
    new QToolButton(QPixmap(const_cast<const char**>(refresh_xpm)),
283
 
                    tr("Refresh list"),
284
 
                    tr("Refresh list"),
285
 
                    this, SLOT(refresh()),
286
 
                    toolbar);
287
 
    toolbar->addSeparator();
288
 
    new QToolButton(QPixmap(const_cast<const char**>(fileopen_xpm)),
289
 
                    tr("Open tracker file"),
290
 
                    tr("Open tracker file"),
291
 
                    this, SLOT(openChart()),
292
 
                    toolbar);
293
 
    new QToolButton(QPixmap(const_cast<const char**>(chart_xpm)),
294
 
                    tr("Setup chart"),
295
 
                    tr("Setup chart"),
296
 
                    this, SLOT(setupChart()),
297
 
                    toolbar);
298
 
 
299
 
    toolbar->setStretchableWidget(new QLabel(toolbar, TO_KDE_TOOLBAR_WIDGET));
300
 
 
301
 
    List = new toListView(this);
302
 
    List->addColumn(tr("Connection"));
303
 
    List->addColumn(tr("Title"));
304
 
    List->addColumn(tr("ID"));
305
 
    List->addColumn(tr("Tracking"));
306
 
    List->addColumn(tr("Alarms"));
307
 
    List->setSorting(2);
308
 
    List->setSelectionMode(QListView::Single);
309
 
 
310
 
    ToolMenu = NULL;
311
 
    connect(toMainWidget()->workspace(), SIGNAL(windowActivated(QWidget *)),
312
 
            this, SLOT(windowActivated(QWidget *)));
313
 
    connect(&Refresh, SIGNAL(timeout()), this, SLOT(refresh()));
314
 
 
315
 
    setFocusProxy(List);
316
 
}
317
 
 
318
 
void toChartManager::windowActivated(QWidget *widget)
319
 
{
320
 
    if (widget == this)
321
 
    {
322
 
        if (!ToolMenu)
323
 
        {
324
 
            ToolMenu = new QPopupMenu(this);
325
 
            ToolMenu->insertItem(QPixmap(const_cast<const char**>(refresh_xpm)), tr("&Refresh"),
326
 
                                 this, SLOT(refresh(void)),
327
 
                                 toKeySequence(tr("F5", "Chart Manager|Refresh")));
328
 
 
329
 
            toMainWidget()->menuBar()->insertItem(tr("&Chart Manager"), ToolMenu, -1, toToolMenuIndex());
330
 
        }
331
 
    }
332
 
    else
333
 
    {
334
 
        delete ToolMenu;
335
 
        ToolMenu = NULL;
336
 
    }
337
 
}
338
 
 
339
 
toChartManager::~toChartManager()
340
 
{
341
 
    ChartTool.closeWindow();
342
 
}
343
 
 
344
 
void toChartHandler::addChart(toLineChart *chart)
345
 
{
346
 
    Charts.insert(Charts.end(), new toChartReceiver(this, chart));
347
 
    toChartManager *manager = ChartTool.manager();
348
 
    if (manager)
349
 
        manager->Refresh.start(1, true);
350
 
}
351
 
 
352
 
void toChartHandler::removeChart(toLineChart *chart)
353
 
{
354
 
    for (std::list<toChartReceiver *>::iterator i = Charts.begin();i != Charts.end();i++)
355
 
    {
356
 
        if ((*i)->chart() == chart)
357
 
        {
358
 
            QString name = (*i)->name();
359
 
            if (!name.isNull())
360
 
            {
361
 
                std::map<QString, std::list<toChartManager::chartAlarm> >::iterator fnda = Alarms.find(name);
362
 
                if (fnda != Alarms.end())
363
 
                {
364
 
                    bool any = false;
365
 
                    for (std::list<toChartManager::chartAlarm>::iterator j = (*fnda).second.begin();j != (*fnda).second.end();)
366
 
                    {
367
 
                        if (!(*j).Persistent)
368
 
                        {
369
 
                            (*fnda).second.erase(j);
370
 
                            j = (*fnda).second.begin();
371
 
                        }
372
 
                        else
373
 
                        {
374
 
                            j++;
375
 
                            any = true;
376
 
                        }
377
 
                    }
378
 
                    if (!any)
379
 
                        Alarms.erase(fnda);
380
 
                }
381
 
 
382
 
                std::map<QString, toChartManager::chartTrack>::iterator fndt = Files.find(name);
383
 
                if (fndt != Files.end())
384
 
                {
385
 
                    if (!(*fndt).second.Persistent)
386
 
                        Files.erase(fndt);
387
 
                }
388
 
            }
389
 
 
390
 
            delete *i;
391
 
            Charts.erase(i);
392
 
            toChartManager *manager = ChartTool.manager();
393
 
            if (manager)
394
 
                manager->Refresh.start(1, true);
395
 
            return ;
396
 
        }
397
 
    }
398
 
}
399
 
 
400
 
toChartManager::alarmSignal::alarmSignal(void)
401
 
{
402
 
    Action = Ignore;
403
 
}
404
 
 
405
 
toChartManager::chartAlarm::chartAlarm(const QString &inp, bool pers)
406
 
{
407
 
    char oper[100];
408
 
    char cols[10000];
409
 
    char comp[100];
410
 
    char act[100];
411
 
    char extra[10000];
412
 
    Signal = false;
413
 
    Persistent = pers;
414
 
 
415
 
    // The \01 is just one character unlikely to be in an email address
416
 
    int ret = sscanf(inp.utf8(), "%s %s %s %lf %s %[^\01]", oper, cols, comp, &Value, act, extra);
417
 
    if (ret != 5 && ret != 6)
418
 
    {
419
 
        Operation = Any;
420
 
        Value = 0;
421
 
        Comparison = Equal;
422
 
        Action = Ignore;
423
 
        return ;
424
 
    }
425
 
 
426
 
    QString t = QString::fromLatin1(oper);
427
 
    if (t == QString::fromLatin1("min"))
428
 
        Operation = Min;
429
 
    else if (t == QString::fromLatin1("all"))
430
 
        Operation = All;
431
 
    else if (t == QString::fromLatin1("sum"))
432
 
        Operation = Sum;
433
 
    else if (t == QString::fromLatin1("average"))
434
 
        Operation = Average;
435
 
    else if (t == QString::fromLatin1("max"))
436
 
        Operation = Max;
437
 
    else
438
 
        Operation = Any;
439
 
 
440
 
    t = QString::fromUtf8(cols);
441
 
    if (t.length() > 2)
442
 
    {
443
 
        QStringList lst = QStringList::split(QString::fromLatin1(","), t.mid(1, t.length() - 2));
444
 
        for (unsigned int i = 0;i < lst.count();i++)
445
 
            Columns.insert(Columns.end(), lst[i].toInt());
446
 
        Columns.sort();
447
 
    }
448
 
 
449
 
    t = QString::fromLatin1(comp);
450
 
    if (t == QString::fromLatin1("="))
451
 
    {
452
 
        Comparison = Equal;
453
 
    }
454
 
    else if (t == QString::fromLatin1("!="))
455
 
    {
456
 
        Comparison = NotEqual;
457
 
    }
458
 
    else if (t == QString::fromLatin1("<"))
459
 
    {
460
 
        Comparison = Less;
461
 
    }
462
 
    else if (t == QString::fromLatin1(">"))
463
 
    {
464
 
        Comparison = Greater;
465
 
    }
466
 
    else if (t == QString::fromLatin1("<="))
467
 
    {
468
 
        Comparison = LessEqual;
469
 
    }
470
 
    else if (t == QString::fromLatin1(">="))
471
 
    {
472
 
        Comparison = GreaterEqual;
473
 
    }
474
 
    else
475
 
    {
476
 
        Comparison = Equal;
477
 
    }
478
 
 
479
 
    t = QString::fromLatin1(act);
480
 
    if (t == QString::fromLatin1("StatusMessage"))
481
 
        Action = StatusMessage;
482
 
    else if (t == QString::fromLatin1("Email"))
483
 
        Action = Email;
484
 
    else
485
 
        Action = Ignore;
486
 
    if (ret == 6)
487
 
        Extra = QString::fromUtf8(extra);
488
 
}
489
 
 
490
 
toChartManager::chartAlarm::chartAlarm()
491
 
{
492
 
    Signal = false;
493
 
    Operation = Any;
494
 
    Comparison = Equal;
495
 
    Action = StatusMessage;
496
 
    Value = 0;
497
 
    Persistent = true;
498
 
}
499
 
 
500
 
toChartManager::chartAlarm::chartAlarm(operation oper, comparison comp, action act,
501
 
                                       double value, std::list<int> &cols, const QString &extra,
502
 
                                       bool persistent)
503
 
{
504
 
    Signal = false;
505
 
    Operation = oper;
506
 
    Comparison = comp;
507
 
    Action = act;
508
 
    Value = value;
509
 
    Columns = cols;
510
 
    Persistent = persistent;
511
 
    Extra = extra;
512
 
}
513
 
 
514
 
QString toChartManager::chartAlarm::toString(void)
515
 
{
516
 
    QString t;
517
 
    switch (Operation)
518
 
    {
519
 
    case Any:
520
 
        t = QString::fromLatin1("any");
521
 
        break;
522
 
    case All:
523
 
        t = QString::fromLatin1("all");
524
 
        break;
525
 
    case Sum:
526
 
        t = QString::fromLatin1("sum");
527
 
        break;
528
 
    case Average:
529
 
        t = QString::fromLatin1("average");
530
 
        break;
531
 
    case Max:
532
 
        t = QString::fromLatin1("max");
533
 
        break;
534
 
    case Min:
535
 
        t = QString::fromLatin1("min");
536
 
        break;
537
 
    }
538
 
    t += QString::fromLatin1(" (");
539
 
    bool first = true;
540
 
    for (std::list<int>::iterator i = Columns.begin();i != Columns.end();i++)
541
 
    {
542
 
        if (first)
543
 
            first = false;
544
 
        else
545
 
            t += QString::fromLatin1(",");
546
 
        t += QString::number(*i);
547
 
    }
548
 
    t += QString::fromLatin1(")");
549
 
    switch (Comparison)
550
 
    {
551
 
    case Equal:
552
 
        t += QString::fromLatin1(" = ");
553
 
        break;
554
 
    case NotEqual:
555
 
        t += QString::fromLatin1(" != ");
556
 
        break;
557
 
    case Less:
558
 
        t += QString::fromLatin1(" < ");
559
 
        break;
560
 
    case Greater:
561
 
        t += QString::fromLatin1(" > ");
562
 
        break;
563
 
    case LessEqual:
564
 
        t += QString::fromLatin1(" <= ");
565
 
        break;
566
 
    case GreaterEqual:
567
 
        t += QString::fromLatin1(" >= ");
568
 
        break;
569
 
    }
570
 
    t += QString::number(Value);
571
 
    switch (Action)
572
 
    {
573
 
    case StatusMessage:
574
 
        t += QString::fromLatin1(" StatusMessage");
575
 
        break;
576
 
    case Email:
577
 
        t += QString::fromLatin1(" Email");
578
 
        break;
579
 
    case Ignore:
580
 
        t += QString::fromLatin1(" Ignore");
581
 
        break;
582
 
    }
583
 
    if (!Extra.isEmpty())
584
 
    {
585
 
        t += QString::fromLatin1(" ");
586
 
        t += Extra;
587
 
    }
588
 
    return t;
589
 
}
590
 
 
591
 
bool toChartManager::chartAlarm::checkValue(double val)
592
 
{
593
 
    switch (Comparison)
594
 
    {
595
 
    case Equal:
596
 
        return val == Value;
597
 
    case NotEqual:
598
 
        return val != Value;
599
 
    case Less:
600
 
        return Value < val;
601
 
    case Greater:
602
 
        return Value > val;
603
 
    case LessEqual:
604
 
        return Value <= val;
605
 
    case GreaterEqual:
606
 
        return Value >= val;
607
 
    }
608
 
    return false;
609
 
}
610
 
 
611
 
void toChartManager::chartAlarm::valueAdded(toChartHandler *handler,
612
 
        const QString &str,
613
 
        std::list<double> &value,
614
 
        const QString &xValue)
615
 
{
616
 
    std::list<double> vals;
617
 
    std::list<double>::iterator i = value.begin();
618
 
    std::list<int>::iterator j = Columns.begin();
619
 
    for (int id = 0;j != Columns.end() && i != value.end();i++, id++)
620
 
    {
621
 
        if (id == *j)
622
 
        {
623
 
            vals.insert(vals.end(), *i);
624
 
            j++;
625
 
        }
626
 
    }
627
 
    bool sig = false;
628
 
    switch (Operation)
629
 
    {
630
 
    case Any:
631
 
        {
632
 
            for (std::list<double>::iterator i = vals.begin();i != vals.end();i++)
633
 
                if (checkValue(*i))
634
 
                {
635
 
                    sig = true;
636
 
                    break;
637
 
                }
638
 
        }
639
 
        break;
640
 
    case All:
641
 
        sig = true;
642
 
        {
643
 
            for (std::list<double>::iterator i = vals.begin();i != vals.end();i++)
644
 
                if (!checkValue(*i))
645
 
                {
646
 
                    sig = false;
647
 
                    break;
648
 
                }
649
 
        }
650
 
        break;
651
 
    case Sum:
652
 
        {
653
 
            double val = 0;
654
 
            for (std::list<double>::iterator i = vals.begin();i != vals.end();i++)
655
 
                val += *i;
656
 
            sig = checkValue(val);
657
 
        }
658
 
        break;
659
 
    case Average:
660
 
        {
661
 
            double val = 0;
662
 
            for (std::list<double>::iterator i = vals.begin();i != vals.end();i++)
663
 
                val += *i;
664
 
            sig = checkValue(val / vals.size());
665
 
        }
666
 
        break;
667
 
    case Max:
668
 
        {
669
 
            bool first = true;
670
 
            double val = 0;
671
 
            for (std::list<double>::iterator i = vals.begin();i != vals.end();i++)
672
 
            {
673
 
                if (first)
674
 
                {
675
 
                    first = false;
676
 
                    val = *i;
677
 
                }
678
 
                else
679
 
                    val = std::max(*i, val);
680
 
            }
681
 
            sig = checkValue(val);
682
 
        }
683
 
        break;
684
 
    case Min:
685
 
        {
686
 
            bool first = true;
687
 
            double val = 0;
688
 
            for (std::list<double>::iterator i = vals.begin();i != vals.end();i++)
689
 
            {
690
 
                if (first)
691
 
                {
692
 
                    first = false;
693
 
                    val = *i;
694
 
                }
695
 
                else
696
 
                    val = std::min(*i, val);
697
 
            }
698
 
            sig = checkValue(val);
699
 
        }
700
 
        break;
701
 
    }
702
 
    if (sig)
703
 
    {
704
 
        if (Signal)
705
 
            return ;
706
 
        handler->SignalAlarms.insert(handler->SignalAlarms.end(),
707
 
                                     alarmSignal(Action, xValue, str, toString(), Extra));
708
 
        handler->Timer.start(1, true);
709
 
        Signal = true;
710
 
    }
711
 
    else
712
 
        Signal = false;
713
 
}
714
 
 
715
 
void toChartHandler::saveSettings(void)
716
 
{
717
 
    {
718
 
        int num = 0;
719
 
        for (std::map<QString, toChartManager::chartTrack>::iterator i = Files.begin();i != Files.end();i++)
720
 
        {
721
 
            if ((*i).second.Persistent)
722
 
            {
723
 
                num++;
724
 
                QCString name = QCString("Files:") + QString::number(num).latin1();
725
 
                ChartTool.setConfig(name + ":Name", (*i).first);
726
 
                ChartTool.setConfig(name + ":Spec", (*i).second.File.name());
727
 
            }
728
 
        }
729
 
        ChartTool.setConfig("FilesCount", QString::number(num));
730
 
    }
731
 
    {
732
 
        int num = 0;
733
 
        for (std::map<QString, std::list<toChartManager::chartAlarm> >::iterator i = Alarms.begin();
734
 
                i != Alarms.end();i++)
735
 
        {
736
 
            for (std::list<toChartManager::chartAlarm>::iterator j = (*i).second.begin();j != (*i).second.end();j++)
737
 
            {
738
 
                if ((*j).Persistent)
739
 
                {
740
 
                    num++;
741
 
                    QCString name = QCString("Alarms:") + QString::number(num).latin1();
742
 
                    ChartTool.setConfig(name + ":Name", (*i).first);
743
 
                    ChartTool.setConfig(name + ":Spec", (*j).toString());
744
 
                }
745
 
            }
746
 
        }
747
 
        ChartTool.setConfig("AlarmCount", QString::number(num));
748
 
    }
749
 
    toTool::saveConfig();
750
 
}
751
 
 
752
 
void toChartHandler::loadSettings(void)
753
 
{
754
 
    {
755
 
        for (int num = ChartTool.config("FilesCount", "0").toInt();num > 0;num--)
756
 
        {
757
 
            QCString name = QCString("Files:") + QString::number(num).latin1();
758
 
            QString t = ChartTool.config(name + ":Name", "");
759
 
            QString s = ChartTool.config(name + ":Spec", "");
760
 
            if (!t.isEmpty() && !s.isEmpty())
761
 
                Files[t] = toChartManager::chartTrack(s, true);
762
 
        }
763
 
    }
764
 
    {
765
 
        for (int num = ChartTool.config("AlarmCount", "0").toInt();num > 0;num--)
766
 
        {
767
 
            QCString name = QCString("Alarms:") + QString::number(num).latin1();
768
 
            QString t = ChartTool.config(name + ":Name", "");
769
 
            QString s = ChartTool.config(name + ":Spec", "");
770
 
            if (!t.isEmpty() && !s.isEmpty())
771
 
                Alarms[t].insert(Alarms[t].end(), toChartManager::chartAlarm(s, true));
772
 
        }
773
 
    }
774
 
}
775
 
 
776
 
void toChartHandler::alarm(void)
777
 
{
778
 
    while (!SignalAlarms.empty())
779
 
    {
780
 
        toChartManager::alarmSignal signal = toShift(SignalAlarms);
781
 
        if (signal.Action == toChartManager::StatusMessage)
782
 
            toStatusMessage(tr("ALARM:") + signal.Chart + QString::fromLatin1(": ") + signal.Alarm + QString::fromLatin1(": ") + signal.xValue);
783
 
        else if (signal.Action == toChartManager::Email)
784
 
            new toSMTP(QString::fromLatin1(TOAPPNAME " <noreply@localhost>"),
785
 
                       signal.Extra,
786
 
                       tr("TOra alert:") + " " + signal.Chart,
787
 
                       tr("A defined alert value was detected:\n\n%1\n\nAt: %2").arg(signal.Alarm).arg(signal.xValue));
788
 
    }
789
 
}
790
 
 
791
 
void toChartHandler::valueAdded(toLineChart *chart,
792
 
                                const QString &chartName,
793
 
                                std::list<double> &value,
794
 
                                const QString &xValue)
795
 
{
796
 
    std::map<QString, std::list<toChartManager::chartAlarm> >::iterator fnda = Alarms.find(chartName);
797
 
    if (fnda != Alarms.end())
798
 
    {
799
 
        std::list<toChartManager::chartAlarm> &alarms = (*fnda).second;
800
 
        for (std::list<toChartManager::chartAlarm>::iterator i = alarms.begin();i != alarms.end();i++)
801
 
            (*i).valueAdded(this, chartName, value, xValue);
802
 
    }
803
 
 
804
 
    std::map<QString, toChartManager::chartTrack>::iterator fndt = Files.find(chartName);
805
 
    if (fndt != Files.end())
806
 
    {
807
 
        QFile &file = (*fndt).second.File;
808
 
        bool header = false;
809
 
        if (!file.isOpen())
810
 
        {
811
 
            if (!file.exists())
812
 
                header = true;
813
 
            file.open(IO_Raw | IO_WriteOnly | IO_Append);
814
 
        }
815
 
        if (file.isOpen())
816
 
        {
817
 
            static QRegExp quote(QString::fromLatin1("\""));
818
 
            QCString out = "\"";
819
 
            if (header)
820
 
            {
821
 
                QString t = chart->title();
822
 
                t.replace(quote, QString::fromLatin1("\"\""));
823
 
                out += t.utf8();
824
 
                std::list<QString> labels = chart->labels();
825
 
                for (std::list<QString>::iterator i = labels.begin();i != labels.end();i++)
826
 
                {
827
 
                    out += "\";\"";
828
 
                    QString t = (*i);
829
 
                    t.replace(quote, QString::fromLatin1("\"\""));
830
 
                    out += t.utf8();
831
 
                }
832
 
                out += "\"\n\"";
833
 
            }
834
 
            QString t = xValue;
835
 
            t.replace(quote, QString::fromLatin1("\"\""));
836
 
            out += t.utf8();
837
 
            for (std::list<double>::iterator i = value.begin();i != value.end();i++)
838
 
            {
839
 
                out += "\";\"";
840
 
                out += QString::number(*i);
841
 
            }
842
 
            out += "\"\n";
843
 
            file.writeBlock(out, out.length());
844
 
        }
845
 
    }
846
 
}
847
 
 
848
 
static QString ReadCSV(const QCString &data, unsigned int &pos, bool &nl)
849
 
{
850
 
    QCString ret;
851
 
    if (data.at(pos) != '\"')
852
 
        throw qApp->translate("toChartManager", "Initial value didn't start with \" in CSV file");
853
 
    pos++;
854
 
    while (pos < data.length() &&
855
 
            (data.at(pos) != '\"' || (pos + 1 < data.length() &&
856
 
                                      data.at(pos + 1) == '\"')))
857
 
    {
858
 
        ret += data.at(pos);
859
 
        if (data.at(pos) == '\"')
860
 
            pos++;
861
 
        pos++;
862
 
    }
863
 
    if (pos >= data.length())
864
 
        throw qApp->translate("toChartManager", "Missing closing \" in CSV file");
865
 
    pos++;
866
 
    nl = false;
867
 
    while (pos < data.length() && (isspace(data.at(pos)) || data.at(pos) == ',' || data.at(pos) == ';'))
868
 
    {
869
 
        if (data.at(pos) == '\n')
870
 
            nl = true;
871
 
        pos++;
872
 
    }
873
 
    return QString::fromUtf8(ret);
874
 
}
875
 
 
876
 
void toChartManager::openChart(void)
877
 
{
878
 
    QString name = toOpenFilename(QString::null, QString::fromLatin1("*.csv"), this);
879
 
    if (!name.isEmpty())
880
 
    {
881
 
        try
882
 
        {
883
 
            QCString data = toReadFile(name);
884
 
            unsigned int pos = 0;
885
 
 
886
 
            toLineChart *chart;
887
 
            switch (TOMessageBox::information(toMainWidget(),
888
 
                                              tr("Chart format"),
889
 
                                              tr("Select format of the chart to display"),
890
 
                                              tr("Barchart"),
891
 
                                              tr("Linechart"),
892
 
                                              tr("Cancel")))
893
 
            {
894
 
            case 0:
895
 
                chart = new toBarChart(toMainWidget()->workspace(), NULL,
896
 
                                       WDestructiveClose);
897
 
                break;
898
 
            case 1:
899
 
                chart = new toLineChart(toMainWidget()->workspace(), NULL,
900
 
                                        WDestructiveClose);
901
 
                break;
902
 
            default:
903
 
                return ;
904
 
            }
905
 
            chart->show();
906
 
            chart->setSamples( -1);
907
 
            bool eol;
908
 
            chart->setTitle(ReadCSV(data, pos, eol));
909
 
            std::list<QString> lab;
910
 
            while (!eol && pos < data.length())
911
 
                lab.insert(lab.end(), ReadCSV(data, pos, eol));
912
 
            chart->setLabels(lab);
913
 
            while (pos < data.length())
914
 
            {
915
 
                QString t = ReadCSV(data, pos, eol);
916
 
                std::list<double> val;
917
 
                while (!eol && pos < data.length())
918
 
                    val.insert(val.end(), ReadCSV(data, pos, eol).toDouble());
919
 
                chart->addValues(val, t);
920
 
            }
921
 
        }
922
 
        TOCATCH
923
 
    }
924
 
}
925
 
 
926
 
void toChartManager::setupChart(void)
927
 
{
928
 
    toChartReceiver *chart = selectedChart();
929
 
    if (chart && ChartTool.handler())
930
 
        ChartTool.handler()->setupChart(chart->chart());
931
 
}
932
 
 
933
 
toChartReceiver *toChartManager::selectedChart(void)
934
 
{
935
 
    try
936
 
    {
937
 
        QListViewItem *item = List->selectedItem();
938
 
        if (item && ChartTool.handler())
939
 
        {
940
 
            for (std::list<toChartReceiver *>::iterator i = ChartTool.handler()->Charts.begin();i != ChartTool.handler()->Charts.end();i++)
941
 
            {
942
 
                toResult *result = (*i)->result();
943
 
                if (result)
944
 
                {
945
 
                    if (item->text(0) == result->connection().description(false) &&
946
 
                            item->text(2) == result->sqlName())
947
 
                        return *i;
948
 
                }
949
 
            }
950
 
        }
951
 
    }
952
 
    TOCATCH
953
 
    return NULL;
954
 
}
955
 
 
956
 
void toChartHandler::setupChart(toLineChart *chart)
957
 
{
958
 
    for (std::list<toChartReceiver *>::iterator i = Charts.begin();i != Charts.end();i++)
959
 
    {
960
 
        if ((*i)->chart() == chart)
961
 
        {
962
 
            QString name = (*i)->name();
963
 
            if (!name.isNull())
964
 
            {
965
 
                std::list<toChartManager::chartAlarm> alarm;
966
 
                std::map<QString, std::list<toChartManager::chartAlarm> >::iterator fnda = Alarms.find(name);
967
 
                if (fnda != Alarms.end())
968
 
                    alarm = (*fnda).second;
969
 
 
970
 
                toChartManager::chartTrack file;
971
 
                std::map<QString, toChartManager::chartTrack>::iterator fndt = Files.find(name);
972
 
                if (fndt != Files.end())
973
 
                    file = (*fndt).second;
974
 
 
975
 
                toChartSetup setup(chart, toMainWidget(), NULL, true);
976
 
                setup.Alarms->addColumn(tr("Alarms"));
977
 
                setup.Alarms->addColumn(tr("Persistent"));
978
 
                setup.Alarms->setSorting(0);
979
 
                setup.Alarms->setSelectionMode(QListView::Single);
980
 
                setup.Persistent->setChecked(file.Persistent);
981
 
                setup.Filename->setText(file.File.name());
982
 
                if (!setup.Filename->text().isEmpty())
983
 
                    setup.Enabled->setChecked(true);
984
 
                setup.ChartFrame->setSizePolicy(QSizePolicy(QSizePolicy::Expanding,
985
 
                                                QSizePolicy::Preferred));
986
 
 
987
 
                toLineChart *t = chart->openCopy(setup.ChartFrame);
988
 
                t->setSizePolicy(QSizePolicy(QSizePolicy::Expanding,
989
 
                                             QSizePolicy::Preferred));
990
 
                t->showLegend(false);
991
 
                t->showAxisLegend(false);
992
 
                for (std::list<toChartManager::chartAlarm>::iterator j = alarm.begin();j != alarm.end();j++)
993
 
                    new QListViewItem(setup.Alarms,
994
 
                                      (*j).toString(),
995
 
                                      (*j).Persistent ? tr("Persistent") : tr("Temporary"));
996
 
 
997
 
                if (setup.exec())
998
 
                {
999
 
                    if (setup.Filename->text().isEmpty() || !setup.Enabled->isChecked())
1000
 
                    {
1001
 
                        if (fndt != Files.end())
1002
 
                            Files.erase(fndt);
1003
 
                    }
1004
 
                    else
1005
 
                    {
1006
 
                        Files[name] = toChartManager::chartTrack(setup.Filename->text(),
1007
 
                                      setup.Persistent->isChecked());
1008
 
                    }
1009
 
                    if (fnda != Alarms.end())
1010
 
                        Alarms.erase(fnda);
1011
 
                    alarm.clear();
1012
 
                    for (QListViewItem *item = setup.Alarms->firstChild();
1013
 
                            item;item = item->nextSibling())
1014
 
                    {
1015
 
                        alarm.insert(alarm.end(), toChartManager::chartAlarm(item->text(0),
1016
 
                                     item->text(1) == tr("Persistent")));
1017
 
                    }
1018
 
                    if (!alarm.empty())
1019
 
                        Alarms[name] = alarm;
1020
 
                    saveSettings();
1021
 
                }
1022
 
            }
1023
 
            break;
1024
 
        }
1025
 
    }
1026
 
}
1027
 
 
1028
 
void toChartManager::refresh(void)
1029
 
{
1030
 
    if (!ChartTool.handler())
1031
 
        return ;
1032
 
    try
1033
 
    {
1034
 
        List->clear();
1035
 
        for (std::list<toChartReceiver *>::iterator i = ChartTool.handler()->Charts.begin();i != ChartTool.handler()->Charts.end();i++)
1036
 
        {
1037
 
            toResult *result = (*i)->result();
1038
 
            if (result)
1039
 
            {
1040
 
                toResultViewItem *item = new toResultViewMLine(List,
1041
 
                                         NULL,
1042
 
                                         result->connection().description(false));
1043
 
                item->setText(1, (*i)->chart()->title());
1044
 
                item->setText(2, result->sqlName());
1045
 
                QString name = (*i)->name();
1046
 
                if (!name.isNull())
1047
 
                {
1048
 
                    std::map<QString, std::list<chartAlarm> >::iterator fnda = ChartTool.handler()->Alarms.find(name);
1049
 
                    if (fnda != ChartTool.handler()->Alarms.end())
1050
 
                    {
1051
 
                        QString t;
1052
 
                        for (std::list<chartAlarm>::iterator j = (*fnda).second.begin();j != (*fnda).second.end();j++)
1053
 
                        {
1054
 
                            t += (*j).toString();
1055
 
                            t += QString::fromLatin1("\n");
1056
 
                        }
1057
 
                        if (t.length() > 0)
1058
 
                            item->setText(4, t.mid(0, t.length() - 1));
1059
 
                    }
1060
 
 
1061
 
                    std::map<QString, chartTrack>::iterator fndt = ChartTool.handler()->Files.find(name);
1062
 
                    if (fndt != ChartTool.handler()->Files.end())
1063
 
                        item->setText(3, (*fndt).second.File.name());
1064
 
                }
1065
 
            }
1066
 
        }
1067
 
    }
1068
 
    TOCATCH
1069
 
}
1070
 
 
1071
 
toChartHandler::toChartHandler()
1072
 
{
1073
 
    connect(toMainWidget(), SIGNAL(chartAdded(toLineChart *)),
1074
 
            this, SLOT(addChart(toLineChart *)));
1075
 
    connect(toMainWidget(), SIGNAL(chartRemoved(toLineChart *)),
1076
 
            this, SLOT(removeChart(toLineChart *)));
1077
 
    connect(toMainWidget(), SIGNAL(chartSetup(toLineChart *)),
1078
 
            this, SLOT(setupChart(toLineChart *)));
1079
 
    connect(&Timer, SIGNAL(timeout()), this, SLOT(alarm()));
1080
 
}
1081
 
 
1082
 
toChartHandler::~toChartHandler()
1083
 
{
1084
 
    ChartTool.closeHandler();
1085
 
}