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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
|
/* $Id$ */
/** @file
* VBox Qt GUI - UIWizardAddCloudVMPageSource class implementation.
*/
/*
* Copyright (C) 2009-2021 Oracle Corporation
*
* This file is part of VirtualBox Open Source Edition (OSE), as
* available from http://www.virtualbox.org. This file is free software;
* you can redistribute it and/or modify it under the terms of the GNU
* General Public License (GPL) as published by the Free Software
* Foundation, in version 2 as it comes in the "COPYING" file of the
* VirtualBox OSE distribution. VirtualBox OSE is distributed in the
* hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
*/
/* Qt includes: */
#include <QGridLayout>
#include <QHeaderView>
#include <QLabel>
#include <QListWidget>
#include <QVBoxLayout>
/* GUI includes: */
#include "QIComboBox.h"
#include "QIRichTextLabel.h"
#include "QIToolButton.h"
#include "UICloudNetworkingStuff.h"
#include "UIIconPool.h"
#include "UIMessageCenter.h"
#include "UIVirtualBoxEventHandler.h"
#include "UIVirtualBoxManager.h"
#include "UIWizardAddCloudVM.h"
#include "UIWizardAddCloudVMPageSource.h"
/* COM includes: */
#include "CStringArray.h"
/* Namespaces: */
using namespace UIWizardAddCloudVMPage1;
/*********************************************************************************************************************************
* Namespace UIWizardAddCloudVMPage1 implementation. *
*********************************************************************************************************************************/
void UIWizardAddCloudVMPage1::populateProviders(QIComboBox *pCombo)
{
/* Sanity check: */
AssertPtrReturnVoid(pCombo);
/* We need top-level parent as well: */
QWidget *pParent = pCombo->window();
AssertPtrReturnVoid(pParent);
/* Block signals while updating: */
pCombo->blockSignals(true);
/* Remember current item data to be able to restore it: */
QString strOldData;
if (pCombo->currentIndex() != -1)
strOldData = pCombo->itemData(pCombo->currentIndex(), ProviderData_ShortName).toString();
/* Otherwise "OCI" should be the default one: */
else
strOldData = "OCI";
/* Clear combo initially: */
pCombo->clear();
/* Iterate through existing providers: */
foreach (const CCloudProvider &comProvider, listCloudProviders(pParent))
{
/* Skip if we have nothing to populate (file missing?): */
if (comProvider.isNull())
continue;
/* Acquire provider name: */
QString strProviderName;
if (!cloudProviderName(comProvider, strProviderName, pParent))
continue;
/* Acquire provider short name: */
QString strProviderShortName;
if (!cloudProviderShortName(comProvider, strProviderShortName, pParent))
continue;
/* Compose empty item, fill the data: */
pCombo->addItem(QString());
pCombo->setItemData(pCombo->count() - 1, strProviderName, ProviderData_Name);
pCombo->setItemData(pCombo->count() - 1, strProviderShortName, ProviderData_ShortName);
}
/* Set previous/default item if possible: */
int iNewIndex = -1;
if ( iNewIndex == -1
&& !strOldData.isNull())
iNewIndex = pCombo->findData(strOldData, ProviderData_ShortName);
if ( iNewIndex == -1
&& pCombo->count() > 0)
iNewIndex = 0;
if (iNewIndex != -1)
pCombo->setCurrentIndex(iNewIndex);
/* Unblock signals after update: */
pCombo->blockSignals(false);
}
void UIWizardAddCloudVMPage1::populateProfiles(QIComboBox *pCombo, const CCloudProvider &comProvider)
{
/* Sanity check: */
AssertPtrReturnVoid(pCombo);
AssertReturnVoid(comProvider.isNotNull());
/* We need top-level parent as well: */
QWidget *pParent = pCombo->window();
AssertPtrReturnVoid(pParent);
/* Block signals while updating: */
pCombo->blockSignals(true);
/* Remember current item data to be able to restore it: */
QString strOldData;
if (pCombo->currentIndex() != -1)
strOldData = pCombo->itemData(pCombo->currentIndex(), ProfileData_Name).toString();
else
{
/* Try to fetch "old" profile name from wizard full group name: */
UIWizardAddCloudVM *pWizard = qobject_cast<UIWizardAddCloudVM*>(pParent);
AssertPtrReturnVoid(pWizard);
const QString strFullGroupName = pWizard->fullGroupName();
const QString strProfileName = strFullGroupName.section('/', 2, 2);
if (!strProfileName.isEmpty())
strOldData = strProfileName;
}
/* Clear combo initially: */
pCombo->clear();
/* Iterate through existing profiles: */
foreach (const CCloudProfile &comProfile, listCloudProfiles(comProvider, pParent))
{
/* Skip if we have nothing to populate (wtf happened?): */
if (comProfile.isNull())
continue;
/* Acquire profile name: */
QString strProfileName;
if (!cloudProfileName(comProfile, strProfileName, pParent))
continue;
/* Compose item, fill the data: */
pCombo->addItem(strProfileName);
pCombo->setItemData(pCombo->count() - 1, strProfileName, ProfileData_Name);
}
/* Set previous/default item if possible: */
int iNewIndex = -1;
if ( iNewIndex == -1
&& !strOldData.isNull())
iNewIndex = pCombo->findData(strOldData, ProfileData_Name);
if ( iNewIndex == -1
&& pCombo->count() > 0)
iNewIndex = 0;
if (iNewIndex != -1)
pCombo->setCurrentIndex(iNewIndex);
/* Unblock signals after update: */
pCombo->blockSignals(false);
}
void UIWizardAddCloudVMPage1::populateProfileInstances(QListWidget *pList, const CCloudClient &comClient)
{
/* Sanity check: */
AssertPtrReturnVoid(pList);
AssertReturnVoid(comClient.isNotNull());
/* We need top-level parent as well: */
QWidget *pParent = pList->window();
AssertPtrReturnVoid(pParent);
/* Block signals while updating: */
pList->blockSignals(true);
/* Clear list initially: */
pList->clear();
/* Gather instance names and ids: */
CStringArray comNames;
CStringArray comIDs;
if (listCloudSourceInstances(comClient, comNames, comIDs, pParent))
{
/* Push acquired names to list rows: */
const QVector<QString> names = comNames.GetValues();
const QVector<QString> ids = comIDs.GetValues();
for (int i = 0; i < names.size(); ++i)
{
/* Create list item: */
QListWidgetItem *pItem = new QListWidgetItem(names.at(i), pList);
if (pItem)
{
pItem->setFlags(pItem->flags() & ~Qt::ItemIsEditable);
pItem->setData(Qt::UserRole, ids.at(i));
}
}
}
/* Choose the 1st one by default if possible: */
if (pList->count())
pList->setCurrentRow(0);
/* Unblock signals after update: */
pList->blockSignals(false);
}
void UIWizardAddCloudVMPage1::updateComboToolTip(QIComboBox *pCombo)
{
/* Sanity check: */
AssertPtrReturnVoid(pCombo);
const int iCurrentIndex = pCombo->currentIndex();
if (iCurrentIndex != -1)
{
const QString strCurrentToolTip = pCombo->itemData(iCurrentIndex, Qt::ToolTipRole).toString();
AssertMsg(!strCurrentToolTip.isEmpty(), ("Tool-tip data not found!\n"));
pCombo->setToolTip(strCurrentToolTip);
}
}
QStringList UIWizardAddCloudVMPage1::currentListWidgetData(QListWidget *pList)
{
QStringList result;
foreach (QListWidgetItem *pItem, pList->selectedItems())
result << pItem->data(Qt::UserRole).toString();
return result;
}
/*********************************************************************************************************************************
* Class UIWizardAddCloudVMPageSource implementation. *
*********************************************************************************************************************************/
UIWizardAddCloudVMPageSource::UIWizardAddCloudVMPageSource()
: m_pLabelMain(0)
, m_pProviderLayout(0)
, m_pProviderLabel(0)
, m_pProviderComboBox(0)
, m_pLabelDescription(0)
, m_pOptionsLayout(0)
, m_pProfileLabel(0)
, m_pProfileComboBox(0)
, m_pProfileToolButton(0)
, m_pSourceInstanceLabel(0)
, m_pSourceInstanceList(0)
{
/* Prepare main layout: */
QVBoxLayout *pLayoutMain = new QVBoxLayout(this);
if (pLayoutMain)
{
/* Prepare main label: */
m_pLabelMain = new QIRichTextLabel(this);
if (m_pLabelMain)
pLayoutMain->addWidget(m_pLabelMain);
/* Prepare provider layout: */
m_pProviderLayout = new QGridLayout;
if (m_pProviderLayout)
{
m_pProviderLayout->setContentsMargins(0, 0, 0, 0);
m_pProviderLayout->setColumnStretch(0, 0);
m_pProviderLayout->setColumnStretch(1, 1);
/* Prepare provider label: */
m_pProviderLabel = new QLabel(this);
if (m_pProviderLabel)
m_pProviderLayout->addWidget(m_pProviderLabel, 0, 0, Qt::AlignRight);
/* Prepare provider combo-box: */
m_pProviderComboBox = new QIComboBox(this);
if (m_pProviderComboBox)
{
m_pProviderLabel->setBuddy(m_pProviderComboBox);
m_pProviderLayout->addWidget(m_pProviderComboBox, 0, 1);
}
/* Add into layout: */
pLayoutMain->addLayout(m_pProviderLayout);
}
/* Prepare description label: */
m_pLabelDescription = new QIRichTextLabel(this);
if (m_pLabelDescription)
pLayoutMain->addWidget(m_pLabelDescription);
/* Prepare options layout: */
m_pOptionsLayout = new QGridLayout;
if (m_pOptionsLayout)
{
m_pOptionsLayout->setContentsMargins(0, 0, 0, 0);
m_pOptionsLayout->setColumnStretch(0, 0);
m_pOptionsLayout->setColumnStretch(1, 1);
m_pOptionsLayout->setRowStretch(1, 0);
m_pOptionsLayout->setRowStretch(2, 1);
/* Prepare profile label: */
m_pProfileLabel = new QLabel(this);
if (m_pProfileLabel)
m_pOptionsLayout->addWidget(m_pProfileLabel, 0, 0, Qt::AlignRight);
/* Prepare profile layout: */
QHBoxLayout *pProfileLayout = new QHBoxLayout;
if (pProfileLayout)
{
pProfileLayout->setContentsMargins(0, 0, 0, 0);
pProfileLayout->setSpacing(1);
/* Prepare profile combo-box: */
m_pProfileComboBox = new QIComboBox(this);
if (m_pProfileComboBox)
{
m_pProfileLabel->setBuddy(m_pProfileComboBox);
pProfileLayout->addWidget(m_pProfileComboBox);
}
/* Prepare profile tool-button: */
m_pProfileToolButton = new QIToolButton(this);
if (m_pProfileToolButton)
{
m_pProfileToolButton->setIcon(UIIconPool::iconSet(":/cloud_profile_manager_16px.png",
":/cloud_profile_manager_disabled_16px.png"));
pProfileLayout->addWidget(m_pProfileToolButton);
}
/* Add into layout: */
m_pOptionsLayout->addLayout(pProfileLayout, 0, 1);
}
/* Prepare source instance label: */
m_pSourceInstanceLabel = new QLabel(this);
if (m_pSourceInstanceLabel)
m_pOptionsLayout->addWidget(m_pSourceInstanceLabel, 1, 0, Qt::AlignRight);
/* Prepare source instances table: */
m_pSourceInstanceList = new QListWidget(this);
if (m_pSourceInstanceList)
{
m_pSourceInstanceLabel->setBuddy(m_pSourceInstanceLabel);
/* Make source image list fit 50 symbols
* horizontally and 8 lines vertically: */
const QFontMetrics fm(m_pSourceInstanceList->font());
const int iFontWidth = fm.width('x');
const int iTotalWidth = 50 * iFontWidth;
const int iFontHeight = fm.height();
const int iTotalHeight = 8 * iFontHeight;
m_pSourceInstanceList->setMinimumSize(QSize(iTotalWidth, iTotalHeight));
m_pSourceInstanceList->setAlternatingRowColors(true);
m_pSourceInstanceList->setSelectionMode(QAbstractItemView::ExtendedSelection);
/* Add into layout: */
m_pOptionsLayout->addWidget(m_pSourceInstanceList, 1, 1, 2, 1);
}
/* Add into layout: */
pLayoutMain->addLayout(m_pOptionsLayout);
}
}
/* Setup connections: */
connect(gVBoxEvents, &UIVirtualBoxEventHandler::sigCloudProfileRegistered,
this, &UIWizardAddCloudVMPageSource::sltHandleProviderComboChange);
connect(gVBoxEvents, &UIVirtualBoxEventHandler::sigCloudProfileChanged,
this, &UIWizardAddCloudVMPageSource::sltHandleProviderComboChange);
connect(m_pProviderComboBox, static_cast<void(QIComboBox::*)(int)>(&QIComboBox::activated),
this, &UIWizardAddCloudVMPageSource::sltHandleProviderComboChange);
connect(m_pProfileComboBox, static_cast<void(QIComboBox::*)(int)>(&QIComboBox::currentIndexChanged),
this, &UIWizardAddCloudVMPageSource::sltHandleProfileComboChange);
connect(m_pProfileToolButton, &QIToolButton::clicked,
this, &UIWizardAddCloudVMPageSource::sltHandleProfileButtonClick);
connect(m_pSourceInstanceList, &QListWidget::currentRowChanged,
this, &UIWizardAddCloudVMPageSource::sltHandleSourceInstanceChange);
}
void UIWizardAddCloudVMPageSource::retranslateUi()
{
/* Translate page: */
setTitle(UIWizardAddCloudVM::tr("Source to add from"));
/* Translate main label: */
m_pLabelMain->setText(UIWizardAddCloudVM::tr("Please choose the source to add cloud virtual machine from. This can "
"be one of known cloud service providers below."));
/* Translate provider label: */
m_pProviderLabel->setText(UIWizardAddCloudVM::tr("&Source:"));
/* Translate received values of Source combo-box.
* We are enumerating starting from 0 for simplicity: */
for (int i = 0; i < m_pProviderComboBox->count(); ++i)
{
m_pProviderComboBox->setItemText(i, m_pProviderComboBox->itemData(i, ProviderData_Name).toString());
m_pProviderComboBox->setItemData(i, UIWizardAddCloudVM::tr("Add VM from cloud service provider."), Qt::ToolTipRole);
}
/* Translate description label: */
m_pLabelDescription->setText(UIWizardAddCloudVM::tr("<p>Please choose one of cloud service profiles you have registered to "
"add virtual machine from. Existing instance list will be "
"updated. To continue, select at least one instance to add virtual "
"machine on the basis of it.</p>"));
/* Translate profile stuff: */
m_pProfileLabel->setText(UIWizardAddCloudVM::tr("&Profile:"));
m_pProfileToolButton->setToolTip(UIWizardAddCloudVM::tr("Open Cloud Profile Manager..."));
m_pSourceInstanceLabel->setText(UIWizardAddCloudVM::tr("&Instances:"));
/* Adjust label widths: */
QList<QWidget*> labels;
labels << m_pProviderLabel;
labels << m_pProfileLabel;
labels << m_pSourceInstanceLabel;
int iMaxWidth = 0;
foreach (QWidget *pLabel, labels)
iMaxWidth = qMax(iMaxWidth, pLabel->minimumSizeHint().width());
m_pProviderLayout->setColumnMinimumWidth(0, iMaxWidth);
m_pOptionsLayout->setColumnMinimumWidth(0, iMaxWidth);
/* Update tool-tips: */
updateComboToolTip(m_pProviderComboBox);
}
void UIWizardAddCloudVMPageSource::initializePage()
{
/* Populate providers: */
populateProviders(m_pProviderComboBox);
/* Translate providers: */
retranslateUi();
/* Fetch it, asynchronously: */
QMetaObject::invokeMethod(this, "sltHandleProviderComboChange", Qt::QueuedConnection);
/* Make image list focused by default: */
m_pSourceInstanceList->setFocus();
}
bool UIWizardAddCloudVMPageSource::isComplete() const
{
/* Initial result: */
bool fResult = true;
/* Make sure client is not NULL and
* at least one instance is selected: */
fResult = client().isNotNull()
&& !instanceIds().isEmpty();
/* Return result: */
return fResult;
}
bool UIWizardAddCloudVMPageSource::validatePage()
{
/* Initial result: */
bool fResult = true;
/* Try to add cloud VMs: */
fResult = qobject_cast<UIWizardAddCloudVM*>(wizard())->addCloudVMs();
/* Return result: */
return fResult;
}
void UIWizardAddCloudVMPageSource::sltHandleProviderComboChange()
{
updateProvider();
emit completeChanged();
}
void UIWizardAddCloudVMPageSource::sltHandleProfileComboChange()
{
updateProfile();
emit completeChanged();
}
void UIWizardAddCloudVMPageSource::sltHandleProfileButtonClick()
{
if (gpManager)
gpManager->openCloudProfileManager();
}
void UIWizardAddCloudVMPageSource::sltHandleSourceInstanceChange()
{
updateSourceInstance();
emit completeChanged();
}
void UIWizardAddCloudVMPageSource::setProviderShortName(const QString &strProviderShortName)
{
qobject_cast<UIWizardAddCloudVM*>(wizard())->setProviderShortName(strProviderShortName);
}
QString UIWizardAddCloudVMPageSource::providerShortName() const
{
return qobject_cast<UIWizardAddCloudVM*>(wizard())->providerShortName();
}
void UIWizardAddCloudVMPageSource::setProfileName(const QString &strProfileName)
{
qobject_cast<UIWizardAddCloudVM*>(wizard())->setProfileName(strProfileName);
}
QString UIWizardAddCloudVMPageSource::profileName() const
{
return qobject_cast<UIWizardAddCloudVM*>(wizard())->profileName();
}
void UIWizardAddCloudVMPageSource::setClient(const CCloudClient &comClient)
{
qobject_cast<UIWizardAddCloudVM*>(wizard())->setClient(comClient);
}
CCloudClient UIWizardAddCloudVMPageSource::client() const
{
return qobject_cast<UIWizardAddCloudVM*>(wizard())->client();
}
void UIWizardAddCloudVMPageSource::setInstanceIds(const QStringList &instanceIds)
{
qobject_cast<UIWizardAddCloudVM*>(wizard())->setInstanceIds(instanceIds);
}
QStringList UIWizardAddCloudVMPageSource::instanceIds() const
{
return qobject_cast<UIWizardAddCloudVM*>(wizard())->instanceIds();
}
void UIWizardAddCloudVMPageSource::updateProvider()
{
updateComboToolTip(m_pProviderComboBox);
setProviderShortName(m_pProviderComboBox->currentData(ProviderData_ShortName).toString());
CCloudProvider comCloudProvider = cloudProviderByShortName(providerShortName(), wizard());
populateProfiles(m_pProfileComboBox, comCloudProvider);
updateProfile();
}
void UIWizardAddCloudVMPageSource::updateProfile()
{
setProfileName(m_pProfileComboBox->currentData(ProfileData_Name).toString());
setClient(cloudClientByName(providerShortName(), profileName(), wizard()));
populateProfileInstances(m_pSourceInstanceList, client());
updateSourceInstance();
}
void UIWizardAddCloudVMPageSource::updateSourceInstance()
{
setInstanceIds(currentListWidgetData(m_pSourceInstanceList));
}
|