~ubuntu-branches/ubuntu/raring/boinc/raring

1.2.5 by Steffen Moeller
Import upstream version 6.13.1+dfsg
1
// This file is part of BOINC.
2
// http://boinc.berkeley.edu
3
// Copyright (C) 2008 University of California
4
//
5
// BOINC is free software; you can redistribute it and/or modify it
6
// under the terms of the GNU Lesser General Public License
7
// as published by the Free Software Foundation,
8
// either version 3 of the License, or (at your option) any later version.
9
//
10
// BOINC is distributed in the hope that it will be useful,
11
// but WITHOUT ANY WARRANTY; without even the implied warranty of
12
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13
// See the GNU Lesser General Public License for more details.
14
//
15
// You should have received a copy of the GNU Lesser General Public License
16
// along with BOINC.  If not, see <http://www.gnu.org/licenses/>.
17
18
19
#include "stdwx.h"
20
#include "Events.h"
21
#include "BOINCGUIApp.h"
22
#include "MainDocument.h"
23
#include "DlgItemProperties.h"
24
#include "sg_TaskPanel.h"
25
#include "sg_TaskCommandPopup.h"
26
27
IMPLEMENT_DYNAMIC_CLASS(CSimpleTaskPopupButton, wxButton)
28
29
BEGIN_EVENT_TABLE(CSimpleTaskPopupButton, wxButton)
30
    EVT_LEFT_DOWN(CSimpleTaskPopupButton::OnTasksCommandButton)
31
    EVT_MENU(ID_TASK_WORK_SHOWGRAPHICS, CSimpleTaskPopupButton::OnTaskShowGraphics)
32
    EVT_MENU(ID_TASK_WORK_SUSPEND, CSimpleTaskPopupButton::OnTaskSuspendResume)
33
    EVT_MENU(ID_TASK_WORK_ABORT, CSimpleTaskPopupButton::OnTaskAbort)
34
    EVT_MENU(ID_TASK_SHOW_PROPERTIES, CSimpleTaskPopupButton::OnTaskShowProperties)
35
END_EVENT_TABLE()
36
37
CSimpleTaskPopupButton::CSimpleTaskPopupButton() {
38
}
39
        
40
CSimpleTaskPopupButton::CSimpleTaskPopupButton(wxWindow* parent, wxWindowID id, 
41
        const wxString& label, const wxPoint& pos, const wxSize& size, 
42
        long style, const wxValidator& validator, const wxString& name) :
43
        wxButton(parent, id, label, pos, size, style, validator, name)
44
    {
45
46
    m_TaskSuspendedViaGUI = false;
47
    m_TaskCommandPopUpMenu = new wxMenu();
48
    AddMenuItems();
49
}
50
        
51
52
CSimpleTaskPopupButton::~CSimpleTaskPopupButton() {
53
    delete m_TaskCommandPopUpMenu;
54
}
55
56
57
void CSimpleTaskPopupButton::AddMenuItems() {
58
    m_ShowGraphicsMenuItem = m_TaskCommandPopUpMenu->Append(
59
        ID_TASK_WORK_SHOWGRAPHICS, 
60
        _("Show graphics"),
61
        _("Show application graphics in a window.")
62
    );
63
64
    m_SuspendResumeMenuItem = m_TaskCommandPopUpMenu->Append(
65
        ID_TASK_WORK_SUSPEND, 
66
        _("Suspend"),
67
        _("Suspend work for this result.")
68
    );
69
70
    m_AbortMenuItem = m_TaskCommandPopUpMenu->Append(
71
        ID_TASK_WORK_ABORT,
72
        _("Abort"),
73
        _("Abandon work on the result. You will get no credit for it.")
74
    );
75
76
    m_ShowPropertiesMenuItem = m_TaskCommandPopUpMenu->Append(
77
        ID_TASK_SHOW_PROPERTIES,
78
        _("Properties"),
79
        _("Show task details.")
80
    );
81
}
82
83
84
void CSimpleTaskPopupButton::OnTasksCommandButton(wxMouseEvent& /*event*/) {
85
    CMainDocument*      pDoc = wxGetApp().GetDocument();
86
    bool                enableShowGraphics = true;
87
    bool                enableAbort = true;
88
    CC_STATUS           status;
89
    wxString            strMachineName;
90
    
91
    wxASSERT(pDoc);
92
    
93
    TaskSelectionData* selData = ((CSimpleTaskPanel*)GetParent())->GetTaskSelectionData();
94
    if (selData == NULL) return;
95
96
    RESULT* result = lookup_result(selData->project_url, selData->result_name);
97
98
    if (!result) return;
99
    
100
    if (result->suspended_via_gui) {
101
        m_TaskSuspendedViaGUI = true;
102
        m_SuspendResumeMenuItem->SetItemLabel(_("Resume"));
103
        m_SuspendResumeMenuItem->SetHelp(_("Resume work for this task."));
104
    } else {
105
        m_TaskSuspendedViaGUI = false;
106
        m_SuspendResumeMenuItem->SetItemLabel(_("Suspend"));
107
        m_SuspendResumeMenuItem->SetHelp(_("Suspend work for this task."));
108
    }
109
110
    pDoc->GetCoreClientStatus(status);
111
    if (status.task_suspend_reason & ~(SUSPEND_REASON_CPU_THROTTLE)) {
112
        enableShowGraphics = false;
113
    }
114
115
    pDoc->GetConnectedComputerName(strMachineName);
116
    if (!pDoc->IsComputerNameLocal(strMachineName)) {
117
        enableShowGraphics = false;
118
    }
119
120
    // Disable Show Graphics button if selected task can't display graphics
121
    if (((!result->supports_graphics) || pDoc->GetState()->executing_as_daemon) 
122
        && !strlen(result->graphics_exec_path)
123
    ) {
124
        enableShowGraphics = false;
125
    }
126
127
    if (result->suspended_via_gui ||
128
        result->project_suspended_via_gui || 
129
        (result->scheduler_state != CPU_SCHED_SCHEDULED)
130
    ) {
131
        enableShowGraphics = false;
132
    }
133
    
134
    m_ShowGraphicsMenuItem->Enable(enableShowGraphics);
135
   
136
    // Disable Abort button if any selected task already aborted
137
    if (
138
        result->active_task_state == PROCESS_ABORT_PENDING ||
139
        result->active_task_state == PROCESS_ABORTED ||
140
        result->state == RESULT_ABORTED 
141
    ) {
142
        enableAbort = false;
143
    }
144
145
    m_AbortMenuItem->Enable(enableAbort);
146
147
#ifdef __WXMAC__
148
    // Disable tooltips on Mac while menus are popped up because they cover menus
149
    wxToolTip::Enable(false);
150
#endif
151
152
	PopupMenu(m_TaskCommandPopUpMenu);
153
154
155
#if TESTBIGICONPOPUP
156
/*** CAF *** FOR TESTING ONLY ***/
157
    static int i;
158
    wxString s;
159
    
160
    if (i > 9) i = 0;
161
    if ( i < 5) {
162
        s = (wxT("This is a very very very and extremely long label."));
163
    } else {
164
        s = (wxT("short."));
165
    }
166
        
167
    switch (i++) {
168
        case 0:
169
        case 5:
170
            UpdateStaticText(&m_TaskProjectName, s);
171
            break;
172
        case 1:
173
        case 6:
174
            UpdateStaticText(&m_TaskApplicationName, s);
175
            break;
176
        case 2:
177
        case 7:
178
            UpdateStaticText(&m_ElapsedTimeValue, s);
179
            break;
180
        case 3:
181
        case 8:
182
            UpdateStaticText(&m_TimeRemainingValue, s);
183
            break;
184
        case 4:
185
        case 9:
186
            UpdateStaticText(&m_StatusValueText, s);
187
            break;
188
    }
189
190
	m_ProgressBar->SetValue( i * 10 ); 
191
    int sel = i % 3;
192
//    m_TaskSelectionCtrl->SetStringSelection(tempArray[sel]);
193
    m_TaskSelectionCtrl->SetSelection(sel);
194
#endif
195
}
196
197
198
void CSimpleTaskPopupButton::OnTaskShowGraphics(wxCommandEvent& WXUNUSED(event)) {
199
    CMainDocument* pDoc     = wxGetApp().GetDocument();
200
201
    wxASSERT(pDoc);
202
    wxASSERT(wxDynamicCast(pDoc, CMainDocument));
203
204
    TaskSelectionData* selData = ((CSimpleTaskPanel*)GetParent())->GetTaskSelectionData();
205
    if (selData == NULL) return;
206
    
207
    RESULT* result = lookup_result(selData->project_url, selData->result_name);
208
    if (result) {
209
        pDoc->WorkShowGraphics(result);
210
    }
211
}
212
213
214
void CSimpleTaskPopupButton::OnTaskSuspendResume(wxCommandEvent& WXUNUSED(event)) {
215
    CMainDocument* pDoc     = wxGetApp().GetDocument();
216
217
    wxASSERT(pDoc);
218
    wxASSERT(wxDynamicCast(pDoc, CMainDocument));
219
220
    TaskSelectionData* selData = ((CSimpleTaskPanel*)GetParent())->GetTaskSelectionData();
221
    if (selData == NULL) return;
222
223
    if (m_TaskSuspendedViaGUI) {
224
        pDoc->WorkResume(selData->project_url, selData->result_name);
225
    } else {
226
        pDoc->WorkSuspend(selData->project_url, selData->result_name);
227
    }
228
}
229
230
231
void CSimpleTaskPopupButton::OnTaskAbort(wxCommandEvent& WXUNUSED(event)) {
232
    wxInt32  iAnswer        = 0;
233
    wxString strMessage     = wxEmptyString;
234
    CMainDocument* pDoc     = wxGetApp().GetDocument();
235
236
    wxASSERT(pDoc);
237
    wxASSERT(wxDynamicCast(pDoc, CMainDocument));
238
239
    if (!pDoc->IsUserAuthorized())  // Probably no longer relevant
240
        return;
241
242
    TaskSelectionData* selData = ((CSimpleTaskPanel*)GetParent())->GetTaskSelectionData();
243
    if (selData == NULL) return;
244
245
    RESULT* result = lookup_result(selData->project_url, selData->result_name);
246
    if (result) {
247
#if SELECTBYRESULTNAME
248
        wxString name = wxString(selData->result_name, wxConvUTF8, strlen(selData->result_name));
249
#else
250
        wxString name = ((CSimpleTaskPanel*)GetParent())->GetSelectedTaskString();
251
#endif
252
        strMessage.Printf(
253
           _("Are you sure you want to abort this task '%s'?\n(Progress: %.1lf%%, Status: %s)"), 
254
           name.c_str(), result->fraction_done * 100.0, result_description(result, false).c_str());
255
256
        iAnswer = wxGetApp().SafeMessageBox(
257
            strMessage,
258
            _("Abort task"),
259
            wxYES_NO | wxICON_QUESTION,
260
            this
261
        );
262
263
        if (wxYES != iAnswer) {
264
            return;
265
        }
266
        
267
        pDoc->WorkAbort(result->project_url, result->name);
268
    }
269
}
270
271
272
void CSimpleTaskPopupButton::OnTaskShowProperties(wxCommandEvent& WXUNUSED(event)) {
273
    TaskSelectionData* selData = ((CSimpleTaskPanel*)GetParent())->GetTaskSelectionData();
274
    if (selData == NULL) return;
275
    
276
    RESULT* result = lookup_result(selData->project_url, selData->result_name);
277
    if (result) {
278
        CDlgItemProperties dlg(this);
279
        dlg.renderInfos(result);
280
        dlg.ShowModal();
281
    }
282
}
283
284
285
// CMainDocument::state.lookup_result() does not yield current scheduler_state; 
286
// we must use CMainDocument::result() for that.
287
RESULT* CSimpleTaskPopupButton::lookup_result(char* url, char* name) {
288
    CMainDocument* pDoc     = wxGetApp().GetDocument();
289
290
    wxASSERT(pDoc);
291
    wxASSERT(wxDynamicCast(pDoc, CMainDocument));
292
293
    return pDoc->result(wxString(name, wxConvUTF8), wxString(url, wxConvUTF8));
294
}