~mapopa/flamerobin/master

« back to all changes in this revision

Viewing changes to src/gui/BackupRestoreBaseFrame.cpp

  • Committer: Michael Hieke
  • Date: 2004-11-22 11:34:42 UTC
  • Revision ID: git-v1:8153b493d66ee7aae55e7b34e0d7bddacf4999ef
Initial revision

svn path=/trunk/flamerobin/; revision=13

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
  The contents of this file are subject to the Initial Developer's Public
 
3
  License Version 1.0 (the "License"); you may not use this file except in
 
4
  compliance with the License. You may obtain a copy of the License here:
 
5
  http://www.flamerobin.org/license.html.
 
6
 
 
7
  Software distributed under the License is distributed on an "AS IS"
 
8
  basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
 
9
  License for the specific language governing rights and limitations under
 
10
  the License.
 
11
 
 
12
  The Original Code is FlameRobin (TM).
 
13
 
 
14
  The Initial Developer of the Original Code is Michael Hieke.
 
15
 
 
16
  Portions created by the original developer
 
17
  are Copyright (C) 2004 Michael Hieke.
 
18
 
 
19
  All Rights Reserved.
 
20
 
 
21
  $Id$
 
22
 
 
23
  Contributor(s): Milan Babuskov
 
24
*/
 
25
 
 
26
// For compilers that support precompilation, includes "wx/wx.h".
 
27
#include "wx/wxprec.h"
 
28
 
 
29
#ifdef __BORLANDC__
 
30
    #pragma hdrstop
 
31
#endif
 
32
 
 
33
// for all others, include the necessary headers (this file is usually all you
 
34
// need because it includes almost all "standard" wxWindows headers
 
35
#ifndef WX_PRECOMP
 
36
    #include "wx/wx.h"
 
37
#endif
 
38
 
 
39
#include <wx/timer.h>
 
40
 
 
41
#include "BackupRestoreBaseFrame.h"
 
42
#include "config.h"
 
43
#include "ugly.h"
 
44
 
 
45
//-----------------------------------------------------------------------------
 
46
BackupRestoreBaseFrame::BackupRestoreBaseFrame(wxWindow* parent, YDatabase* db):
 
47
    BaseFrame(parent, -1, _T(""), wxDefaultPosition, wxDefaultSize, wxDEFAULT_FRAME_STYLE|wxNO_FULL_REPAINT_ON_RESIZE)
 
48
{
 
49
    databaseM = db;
 
50
    serverM = reinterpret_cast<YServer*>(db->getParent());
 
51
 
 
52
        threadM = 0;
 
53
    threadMsgTimeMillisM = 0;
 
54
    verboseMsgsM = true;
 
55
    storageNameM = "unassigned";
 
56
 
 
57
    // create controls in constructor of descendant class to allow for correct tab order
 
58
    panel_controls = 0;
 
59
    label_filename = 0;
 
60
    text_ctrl_filename = 0;
 
61
    button_browse = 0;
 
62
    combobox_showlog = 0;
 
63
    button_start = 0;
 
64
    button_cancel = 0;
 
65
    text_ctrl_log = 0;
 
66
 
 
67
        #include "backup.xpm"
 
68
    wxBitmap bmp(backup_xpm);
 
69
    wxIcon icon;
 
70
    icon.CopyFromBitmap(bmp);
 
71
    SetIcon(icon);
 
72
}
 
73
//-----------------------------------------------------------------------------
 
74
//! some controls need additional setup after descendant frame's controls are created
 
75
void BackupRestoreBaseFrame::setupControls()
 
76
{
 
77
        text_ctrl_log->StyleSetForeground(1, *wxBLUE);
 
78
        text_ctrl_log->StyleSetForeground(2, *wxRED);
 
79
        text_ctrl_log->SetMarginWidth(1, 0);
 
80
        text_ctrl_log->SetWrapMode(wxSTC_WRAP_WORD);
 
81
}
 
82
//-----------------------------------------------------------------------------
 
83
//! implementation details
 
84
void BackupRestoreBaseFrame::addThreadMsg(const wxString msg, bool& notificationNeeded)
 
85
{
 
86
    notificationNeeded = false;
 
87
    wxLongLong millisNow = ::wxGetLocalTimeMillis();
 
88
 
 
89
    wxCriticalSectionLocker locker(critsectM);
 
90
    threadMsgsM.Add(msg);
 
91
    // we post no more than 10 events per second to prevent flooding of the message queue
 
92
    // and keep the frame responsive for user interaction
 
93
    if ((millisNow - threadMsgTimeMillisM).GetLo() > 100)
 
94
    {
 
95
        threadMsgTimeMillisM = millisNow;
 
96
        notificationNeeded = true;
 
97
    }
 
98
}
 
99
//-----------------------------------------------------------------------------
 
100
void BackupRestoreBaseFrame::cancelBackupRestore()
 
101
{
 
102
    if (threadM != 0)
 
103
    {
 
104
        threadM->Delete();
 
105
        threadM = 0;
 
106
    }
 
107
}
 
108
//-----------------------------------------------------------------------------
 
109
void BackupRestoreBaseFrame::clearLog()
 
110
{
 
111
    msgKindsM.Clear();
 
112
    msgsM.Clear();
 
113
    text_ctrl_log->ClearAll();
 
114
}
 
115
//-----------------------------------------------------------------------------
 
116
bool BackupRestoreBaseFrame::Destroy()
 
117
{
 
118
    cancelBackupRestore();
 
119
    return BaseFrame::Destroy();
 
120
}
 
121
//-----------------------------------------------------------------------------
 
122
void BackupRestoreBaseFrame::doReadConfigSettings(const std::string& prefix)
 
123
{
 
124
        BaseFrame::doReadConfigSettings(prefix);
 
125
 
 
126
    bool verbose;
 
127
        if (!config().getValue(prefix + "::verboselog", verbose))
 
128
        verbose = true;
 
129
    combobox_showlog->SetValue(verbose);
 
130
 
 
131
    std::string bkfile;
 
132
    if (config().getValue(prefix + "::backupfilename", bkfile) && !bkfile.empty())
 
133
        text_ctrl_filename->SetValue(std2wx(bkfile));
 
134
}
 
135
//-----------------------------------------------------------------------------
 
136
void BackupRestoreBaseFrame::doWriteConfigSettings(const std::string& prefix) const
 
137
{
 
138
        BaseFrame::doWriteConfigSettings(prefix);
 
139
        config().setValue(prefix + "::verboselog", combobox_showlog->GetValue());
 
140
    config().setValue(prefix + "::backupfilename", wx2std(text_ctrl_filename->GetValue()));
 
141
}
 
142
//-----------------------------------------------------------------------------
 
143
const std::string BackupRestoreBaseFrame::getStorageName() const
 
144
{
 
145
        if (storageNameM == "unassigned")
 
146
    {
 
147
        StorageGranularity g;
 
148
            if (!config().getValue(getName() + "StorageGranularity", g))
 
149
                    g = sgFrame;
 
150
 
 
151
        switch (g)
 
152
            {
 
153
            case sgFrame:
 
154
                    storageNameM = getName();
 
155
                break;
 
156
                    case sgObject:
 
157
                storageNameM = getName() + "::" + databaseM->getItemPath();
 
158
                break;
 
159
                    default:
 
160
                            storageNameM = "";
 
161
                break;
 
162
            }
 
163
    }
 
164
    return storageNameM;
 
165
}
 
166
//-----------------------------------------------------------------------------
 
167
bool BackupRestoreBaseFrame::startThread(wxThread* thread)
 
168
{
 
169
    wxASSERT(threadM == 0);
 
170
    if (wxTHREAD_NO_ERROR != thread->Create())
 
171
    {
 
172
        ::wxMessageBox(_("Error creating thread!"), _("Error"), wxICON_ERROR);
 
173
        delete thread;
 
174
        return false;
 
175
    }
 
176
    if (wxTHREAD_NO_ERROR != thread->Run())
 
177
    {
 
178
        ::wxMessageBox(_("Error starting thread!"), _("Error"), wxICON_ERROR);
 
179
        delete thread;
 
180
        return false;
 
181
    }
 
182
    threadM = thread;
 
183
    return true;
 
184
}
 
185
//-----------------------------------------------------------------------------
 
186
void BackupRestoreBaseFrame::threadOutputMsg(const wxString msg, MsgKind kind)
 
187
{
 
188
    wxString s(msg);
 
189
    switch (kind)
 
190
    {
 
191
        case error_message:
 
192
            s.Prepend(wxT("e"));
 
193
            break;
 
194
        case important_message:
 
195
            s.Prepend(wxT("i"));
 
196
            break;
 
197
        case progress_message:
 
198
            s.Prepend(wxT("p"));
 
199
            break;
 
200
        default:
 
201
            wxASSERT(false);
 
202
            return;
 
203
    }
 
204
    bool doPostMsg = false;
 
205
    addThreadMsg(s, doPostMsg);
 
206
    if (doPostMsg)
 
207
    {
 
208
        wxCommandEvent event(wxEVT_COMMAND_MENU_SELECTED, ID_thread_output);
 
209
        wxPostEvent(this, event);
 
210
    }
 
211
}
 
212
//-----------------------------------------------------------------------------
 
213
void BackupRestoreBaseFrame::updateMessages(size_t firstmsg, size_t lastmsg)
 
214
{
 
215
    if (lastmsg > msgsM.GetCount())
 
216
        lastmsg = msgsM.GetCount();
 
217
    bool added = false;
 
218
    for (size_t i = firstmsg; i < lastmsg; i++)
 
219
    {
 
220
                int style = 0;
 
221
        switch ((MsgKind)msgKindsM[i])
 
222
        {
 
223
            case progress_message:
 
224
                if (!verboseMsgsM)
 
225
                    continue;
 
226
                break;
 
227
            case important_message:
 
228
                                style = 1;
 
229
                break;
 
230
            case error_message:
 
231
                                style = 2;
 
232
                break;
 
233
        }
 
234
                int startpos = text_ctrl_log->GetLength();
 
235
                text_ctrl_log->AddText(msgsM[i] + wxT("\n"));
 
236
                int endpos = text_ctrl_log->GetLength();
 
237
                text_ctrl_log->StartStyling(startpos, 255);
 
238
                text_ctrl_log->SetStyling(endpos-startpos-1, style);
 
239
        added = true;
 
240
    }
 
241
 
 
242
    if (added)
 
243
                text_ctrl_log->GotoPos(text_ctrl_log->GetLength());
 
244
}
 
245
//-----------------------------------------------------------------------------
 
246
//! event handlers
 
247
BEGIN_EVENT_TABLE(BackupRestoreBaseFrame, BaseFrame)
 
248
    EVT_BUTTON(BackupRestoreBaseFrame::ID_button_cancel, BackupRestoreBaseFrame::OnCancelButtonClick)
 
249
    EVT_CHECKBOX(BackupRestoreBaseFrame::ID_checkbox_showlog, BackupRestoreBaseFrame::OnVerboseLogChange)
 
250
    EVT_MENU(BackupRestoreBaseFrame::ID_thread_finished, BackupRestoreBaseFrame::OnThreadFinished)
 
251
    EVT_MENU(BackupRestoreBaseFrame::ID_thread_output, BackupRestoreBaseFrame::OnThreadOutput)
 
252
    EVT_TEXT(BackupRestoreBaseFrame::ID_text_ctrl_filename, BackupRestoreBaseFrame::OnSettingsChange)
 
253
END_EVENT_TABLE()
 
254
//-----------------------------------------------------------------------------
 
255
void BackupRestoreBaseFrame::OnCancelButtonClick(wxCommandEvent& WXUNUSED(event))
 
256
{
 
257
    cancelBackupRestore();
 
258
    updateControls();
 
259
}
 
260
//-----------------------------------------------------------------------------
 
261
void BackupRestoreBaseFrame::OnSettingsChange(wxCommandEvent& WXUNUSED(event))
 
262
{
 
263
        if (IsShown())
 
264
                updateControls();
 
265
}
 
266
//-----------------------------------------------------------------------------
 
267
void BackupRestoreBaseFrame::OnThreadFinished(wxCommandEvent& event)
 
268
{
 
269
    threadM = 0;
 
270
    OnThreadOutput(event);
 
271
    updateControls();
 
272
}
 
273
//-----------------------------------------------------------------------------
 
274
void BackupRestoreBaseFrame::OnThreadOutput(wxCommandEvent& WXUNUSED(event))
 
275
{
 
276
    wxCriticalSectionLocker locker(critsectM);
 
277
    threadMsgTimeMillisM = ::wxGetLocalTimeMillis();
 
278
 
 
279
    size_t first = msgsM.GetCount();
 
280
    for (size_t i = 0; i < threadMsgsM.GetCount(); i++)
 
281
    {
 
282
        wxString s(threadMsgsM[i]);
 
283
        if (s.Length() == 0)
 
284
            continue;
 
285
        switch (s.GetChar(0))
 
286
        {
 
287
            case 'e':
 
288
                msgKindsM.Add((int)error_message);
 
289
                break;
 
290
            case 'i':
 
291
                msgKindsM.Add((int)important_message);
 
292
                break;
 
293
            case 'p':
 
294
                msgKindsM.Add((int)progress_message);
 
295
                break;
 
296
        }
 
297
        // this depends on server type, so just in case...
 
298
        if (s.Last() == '\n')
 
299
            s.RemoveLast();
 
300
        msgsM.Add(s.Mid(1));
 
301
    }
 
302
    threadMsgsM.Clear();
 
303
 
 
304
    updateMessages(first, msgsM.GetCount());
 
305
}
 
306
//-----------------------------------------------------------------------------
 
307
void BackupRestoreBaseFrame::OnVerboseLogChange(wxCommandEvent& WXUNUSED(event))
 
308
{
 
309
        wxBusyCursor wait;
 
310
    verboseMsgsM = combobox_showlog->IsChecked();
 
311
 
 
312
    text_ctrl_log->Freeze();
 
313
    text_ctrl_log->ClearAll();
 
314
    updateMessages(0, msgsM.GetCount());
 
315
    text_ctrl_log->Thaw();
 
316
}
 
317
//-----------------------------------------------------------------------------