~ubuntu-branches/ubuntu/raring/virtualbox-ose/raring

« back to all changes in this revision

Viewing changes to src/VBox/Frontends/VirtualBox/src/settings/vm/VBoxVMSettingsSF.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Felix Geyer
  • Date: 2011-01-30 23:27:25 UTC
  • mfrom: (0.3.12 upstream)
  • Revision ID: james.westby@ubuntu.com-20110130232725-2ouajjd2ggdet0zd
Tags: 4.0.2-dfsg-1ubuntu1
* Merge from Debian unstable, remaining changes:
  - Add Apport hook.
    - debian/virtualbox-ose.files/source_virtualbox-ose.py
    - debian/virtualbox-ose.install
  - Drop *-source packages.
* Drop ubuntu-01-fix-build-gcc45.patch, fixed upstream.
* Drop ubuntu-02-as-needed.patch, added to the Debian package.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* $Id: VBoxVMSettingsSF.cpp $ */
2
 
/** @file
3
 
 *
4
 
 * VBox frontends: Qt4 GUI ("VirtualBox"):
5
 
 * VBoxVMSettingsSF class implementation
6
 
 */
7
 
 
8
 
/*
9
 
 * Copyright (C) 2008-2009 Oracle Corporation
10
 
 *
11
 
 * This file is part of VirtualBox Open Source Edition (OSE), as
12
 
 * available from http://www.virtualbox.org. This file is free software;
13
 
 * you can redistribute it and/or modify it under the terms of the GNU
14
 
 * General Public License (GPL) as published by the Free Software
15
 
 * Foundation, in version 2 as it comes in the "COPYING" file of the
16
 
 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
17
 
 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
18
 
 */
19
 
 
20
 
/* Global includes */
21
 
#include <QHeaderView>
22
 
#include <QTimer>
23
 
 
24
 
/* Local includes */
25
 
#include "VBoxVMSettingsSF.h"
26
 
#include "VBoxVMSettingsSFDetails.h"
27
 
#include "VBoxGlobal.h"
28
 
#include "VBoxProblemReporter.h"
29
 
#include "VBoxUtils.h"
30
 
 
31
 
class SFTreeViewItem : public QTreeWidgetItem
32
 
{
33
 
public:
34
 
 
35
 
    enum { SFTreeViewItemType = QTreeWidgetItem::UserType + 1 };
36
 
 
37
 
    enum FormatType
38
 
    {
39
 
        IncorrectFormat = 0,
40
 
        EllipsisStart   = 1,
41
 
        EllipsisMiddle  = 2,
42
 
        EllipsisEnd     = 3,
43
 
        EllipsisFile    = 4
44
 
    };
45
 
 
46
 
    /* Root Item */
47
 
    SFTreeViewItem (QTreeWidget *aParent, const QStringList &aFields, FormatType aFormat)
48
 
        : QTreeWidgetItem (aParent, aFields, SFTreeViewItemType), mFormat (aFormat)
49
 
    {
50
 
        setFirstColumnSpanned (true);
51
 
        setFlags (flags() ^ Qt::ItemIsSelectable);
52
 
    }
53
 
 
54
 
    /* Child Item */
55
 
    SFTreeViewItem (SFTreeViewItem *aParent, const QStringList &aFields, FormatType aFormat)
56
 
        : QTreeWidgetItem (aParent, aFields, SFTreeViewItemType), mFormat (aFormat)
57
 
    {
58
 
        updateText (aFields);
59
 
    }
60
 
 
61
 
    bool operator< (const QTreeWidgetItem &aOther) const
62
 
    {
63
 
        /* Root items should always been sorted by id-field. */
64
 
        return parent() ? text (0).toLower() < aOther.text (0).toLower() :
65
 
                          text (1).toLower() < aOther.text (1).toLower();
66
 
    }
67
 
 
68
 
    SFTreeViewItem* child (int aIndex) const
69
 
    {
70
 
        QTreeWidgetItem *item = QTreeWidgetItem::child (aIndex);
71
 
        return item && item->type() == SFTreeViewItemType ? static_cast <SFTreeViewItem*> (item) : 0;
72
 
    }
73
 
 
74
 
    QString getText (int aIndex) const
75
 
    {
76
 
        return aIndex >= 0 && aIndex < (int)mTextList.size() ? mTextList [aIndex] : QString::null;
77
 
    }
78
 
 
79
 
    void updateText (const QStringList &aFields)
80
 
    {
81
 
        mTextList.clear();
82
 
        mTextList << aFields;
83
 
        adjustText();
84
 
    }
85
 
 
86
 
    void adjustText()
87
 
    {
88
 
        for (int i = 0; i < treeWidget()->columnCount(); ++ i)
89
 
            processColumn (i);
90
 
    }
91
 
 
92
 
private:
93
 
 
94
 
    void processColumn (int aColumn)
95
 
    {
96
 
        QString oneString = getText (aColumn);
97
 
        if (oneString.isNull())
98
 
            return;
99
 
        QFontMetrics fm = treeWidget()->fontMetrics();
100
 
        int oldSize = fm.width (oneString);
101
 
        int indentSize = fm.width (" ... ");
102
 
        int itemIndent = parent() ? treeWidget()->indentation() * 2 : treeWidget()->indentation();
103
 
        if (aColumn == 0)
104
 
            indentSize += itemIndent;
105
 
        int cWidth = treeWidget()->columnWidth (aColumn);
106
 
 
107
 
        /* Compress text */
108
 
        int start = 0;
109
 
        int finish = 0;
110
 
        int position = 0;
111
 
        int textWidth = 0;
112
 
        do
113
 
        {
114
 
            textWidth = fm.width (oneString);
115
 
            if (textWidth + indentSize > cWidth)
116
 
            {
117
 
                start  = 0;
118
 
                finish = oneString.length();
119
 
 
120
 
                /* Selecting remove position */
121
 
                switch (mFormat)
122
 
                {
123
 
                    case EllipsisStart:
124
 
                        position = start;
125
 
                        break;
126
 
                    case EllipsisMiddle:
127
 
                        position = (finish - start) / 2;
128
 
                        break;
129
 
                    case EllipsisEnd:
130
 
                        position = finish - 1;
131
 
                        break;
132
 
                    case EllipsisFile:
133
 
                    {
134
 
                        QRegExp regExp ("([\\\\/][^\\\\^/]+[\\\\/]?$)");
135
 
                        int newFinish = regExp.indexIn (oneString);
136
 
                        if (newFinish != -1)
137
 
                            finish = newFinish;
138
 
                        position = (finish - start) / 2;
139
 
                        break;
140
 
                    }
141
 
                    default:
142
 
                        AssertMsgFailed (("Invalid format type\n"));
143
 
                }
144
 
 
145
 
                if (position == finish)
146
 
                   break;
147
 
 
148
 
                oneString.remove (position, 1);
149
 
            }
150
 
        }
151
 
        while (textWidth + indentSize > cWidth);
152
 
 
153
 
        if (position || mFormat == EllipsisFile) oneString.insert (position, "...");
154
 
        int newSize = fm.width (oneString);
155
 
        setText (aColumn, newSize < oldSize ? oneString : mTextList [aColumn]);
156
 
        setToolTip (aColumn, text (aColumn) == getText (aColumn) ? QString::null : getText (aColumn));
157
 
 
158
 
        /* Calculate item's size-hint */
159
 
        setSizeHint (aColumn, QSize (fm.width (QString ("  %1  ").arg (getText (aColumn))), 100));
160
 
    }
161
 
 
162
 
    FormatType  mFormat;
163
 
    QStringList mTextList;
164
 
};
165
 
 
166
 
VBoxVMSettingsSF::VBoxVMSettingsSF (int aType, QWidget *aParent)
167
 
    : VBoxSettingsPage (aParent)
168
 
    , mDialogType (aType)
169
 
    , mIsListViewChanged (false)
170
 
{
171
 
    /* Apply UI decorations */
172
 
    Ui::VBoxVMSettingsSF::setupUi (this);
173
 
 
174
 
    /* Prepare actions */
175
 
    mNewAction = new QAction (this);
176
 
    mEdtAction = new QAction (this);
177
 
    mDelAction = new QAction (this);
178
 
 
179
 
    mNewAction->setShortcut (QKeySequence ("Ins"));
180
 
    mEdtAction->setShortcut (QKeySequence ("Ctrl+Space"));
181
 
    mDelAction->setShortcut (QKeySequence ("Del"));
182
 
 
183
 
    mNewAction->setIcon (VBoxGlobal::iconSet (":/add_shared_folder_16px.png",
184
 
                                              ":/add_shared_folder_disabled_16px.png"));
185
 
    mEdtAction->setIcon (VBoxGlobal::iconSet (":/edit_shared_folder_16px.png",
186
 
                                              ":/edit_shared_folder_disabled_16px.png"));
187
 
    mDelAction->setIcon (VBoxGlobal::iconSet (":/revome_shared_folder_16px.png",
188
 
                                              ":/revome_shared_folder_disabled_16px.png"));
189
 
 
190
 
    /* Prepare toolbar */
191
 
    mTbFolders->setUsesTextLabel (false);
192
 
    mTbFolders->setIconSize (QSize (16, 16));
193
 
    mTbFolders->setOrientation (Qt::Vertical);
194
 
    mTbFolders->addAction (mNewAction);
195
 
    mTbFolders->addAction (mEdtAction);
196
 
    mTbFolders->addAction (mDelAction);
197
 
 
198
 
    /* Setup connections */
199
 
    mTwFolders->header()->setMovable (false);
200
 
    connect (mNewAction, SIGNAL (triggered (bool)), this, SLOT (addTriggered()));
201
 
    connect (mEdtAction, SIGNAL (triggered (bool)), this, SLOT (edtTriggered()));
202
 
    connect (mDelAction, SIGNAL (triggered (bool)), this, SLOT (delTriggered()));
203
 
    connect (mTwFolders, SIGNAL (currentItemChanged (QTreeWidgetItem *, QTreeWidgetItem *)),
204
 
             this, SLOT (processCurrentChanged (QTreeWidgetItem *)));
205
 
    connect (mTwFolders, SIGNAL (itemDoubleClicked (QTreeWidgetItem *, int)),
206
 
             this, SLOT (processDoubleClick (QTreeWidgetItem *)));
207
 
    connect (mTwFolders, SIGNAL (customContextMenuRequested (const QPoint &)),
208
 
             this, SLOT (showContextMenu (const QPoint &)));
209
 
 
210
 
    /* Create mTwFolders root items */
211
 
#if 0
212
 
    if (aType == GlobalType)
213
 
    {
214
 
        QStringList fields;
215
 
        fields << tr (" Global Folders") /* name */ << QString::number (GlobalType) /* key */;
216
 
        new SFTreeViewItem (mTwFolders, fields, SFTreeViewItem::EllipsisEnd);
217
 
    }
218
 
#endif
219
 
    if (aType & MachineType)
220
 
    {
221
 
        QStringList fields;
222
 
        fields << tr (" Machine Folders") /* name */ << QString::number (MachineType) /* key */;
223
 
        new SFTreeViewItem (mTwFolders, fields, SFTreeViewItem::EllipsisEnd);
224
 
    }
225
 
    if (aType & ConsoleType)
226
 
    {
227
 
        QStringList fields;
228
 
        fields << tr (" Transient Folders") /* name */ << QString::number (ConsoleType) /* key */;
229
 
        new SFTreeViewItem (mTwFolders, fields, SFTreeViewItem::EllipsisEnd);
230
 
    }
231
 
    mTwFolders->sortItems (0, Qt::AscendingOrder);
232
 
 
233
 
    retranslateUi();
234
 
}
235
 
 
236
 
void VBoxVMSettingsSF::getFromGlobal()
237
 
{
238
 
    AssertMsgFailed (("Global shared folders are not supported now!\n"));
239
 
#if 0
240
 
    SFTreeViewItem *root = searchRoot (true, GlobalType);
241
 
    root->setHidden (false);
242
 
    getFrom (vboxGlobal().virtualBox().GetSharedFolders(), root);
243
 
#endif
244
 
}
245
 
 
246
 
void VBoxVMSettingsSF::getFromMachine (const CMachine &aMachine)
247
 
{
248
 
    mMachine = aMachine;
249
 
    SFTreeViewItem *root = searchRoot (true, MachineType);
250
 
    root->setHidden (false);
251
 
    getFrom (mMachine.GetSharedFolders(), root);
252
 
}
253
 
 
254
 
void VBoxVMSettingsSF::getFromConsole (const CConsole &aConsole)
255
 
{
256
 
    mConsole = aConsole;
257
 
    SFTreeViewItem *root = searchRoot (true, ConsoleType);
258
 
    root->setHidden (false);
259
 
    getFrom (mConsole.GetSharedFolders(), root);
260
 
}
261
 
 
262
 
void VBoxVMSettingsSF::putBackToGlobal()
263
 
{
264
 
    AssertMsgFailed (("Global shared folders are not supported now!\n"));
265
 
#if 0
266
 
    if (!mIsListViewChanged)
267
 
        return;
268
 
    /* This function is only available for GlobalType dialog */
269
 
    Assert (mDialogType == GlobalType);
270
 
    /* Searching for GlobalType item's root */
271
 
    SFTreeViewItem *root = searchRoot (true, GlobalType);
272
 
    Assert (root);
273
 
    CSharedFolderVector vec = vboxGlobal().virtualBox().GetSharedFolders();
274
 
    putBackTo (vec, root);
275
 
#endif
276
 
}
277
 
 
278
 
void VBoxVMSettingsSF::putBackToMachine()
279
 
{
280
 
    if (!mIsListViewChanged)
281
 
        return;
282
 
    /* This function is only available for MachineType dialog */
283
 
    Assert (mDialogType & MachineType);
284
 
    /* Searching for MachineType item's root */
285
 
    SFTreeViewItem *root = searchRoot (true,  MachineType);
286
 
    Assert (root);
287
 
    CSharedFolderVector sfvec = mMachine.GetSharedFolders();
288
 
    putBackTo (sfvec, root);
289
 
}
290
 
 
291
 
void VBoxVMSettingsSF::putBackToConsole()
292
 
{
293
 
    if (!mIsListViewChanged)
294
 
        return;
295
 
    /* This function is only available for ConsoleType dialog */
296
 
    Assert (mDialogType & ConsoleType);
297
 
    /* Searching for ConsoleType item's root */
298
 
    SFTreeViewItem *root = searchRoot (true, ConsoleType);
299
 
    Assert (root);
300
 
    CSharedFolderVector sfvec = mConsole.GetSharedFolders();
301
 
    putBackTo (sfvec, root);
302
 
}
303
 
 
304
 
int VBoxVMSettingsSF::dialogType() const
305
 
{
306
 
     return mDialogType;
307
 
}
308
 
 
309
 
void VBoxVMSettingsSF::getFrom (const CMachine &aMachine)
310
 
{
311
 
    getFromMachine (aMachine);
312
 
}
313
 
 
314
 
void VBoxVMSettingsSF::putBackTo()
315
 
{
316
 
    putBackToMachine();
317
 
}
318
 
 
319
 
void VBoxVMSettingsSF::setOrderAfter (QWidget *aWidget)
320
 
{
321
 
    setTabOrder (aWidget, mTwFolders);
322
 
}
323
 
 
324
 
void VBoxVMSettingsSF::retranslateUi()
325
 
{
326
 
    /* Translate uic generated strings */
327
 
    Ui::VBoxVMSettingsSF::retranslateUi (this);
328
 
 
329
 
    mNewAction->setText (tr ("&Add Shared Folder"));
330
 
    mEdtAction->setText (tr ("&Edit Shared Folder"));
331
 
    mDelAction->setText (tr ("&Remove Shared Folder"));
332
 
 
333
 
    mNewAction->setToolTip (mNewAction->text().remove ('&') +
334
 
        QString (" (%1)").arg (mNewAction->shortcut().toString()));
335
 
    mEdtAction->setToolTip (mEdtAction->text().remove ('&') +
336
 
        QString (" (%1)").arg (mEdtAction->shortcut().toString()));
337
 
    mDelAction->setToolTip (mDelAction->text().remove ('&') +
338
 
        QString (" (%1)").arg (mDelAction->shortcut().toString()));
339
 
 
340
 
    mNewAction->setWhatsThis (tr ("Adds a new shared folder definition."));
341
 
    mEdtAction->setWhatsThis (tr ("Edits the selected shared folder definition."));
342
 
    mDelAction->setWhatsThis (tr ("Removes the selected shared folder definition."));
343
 
 
344
 
    mTrFull = tr ("Full");
345
 
    mTrReadOnly = tr ("Read-only");
346
 
}
347
 
 
348
 
void VBoxVMSettingsSF::addTriggered()
349
 
{
350
 
    /* Invoke Add-Box Dialog */
351
 
    VBoxVMSettingsSFDetails dlg (VBoxVMSettingsSFDetails::AddType, mDialogType & ConsoleType, usedList (true), this);
352
 
    if (dlg.exec() == QDialog::Accepted)
353
 
    {
354
 
        QString name = dlg.name();
355
 
        QString path = dlg.path();
356
 
        bool isPermanent = dlg.isPermanent();
357
 
        /* Shared folder's name & path could not be empty */
358
 
        Assert (!name.isEmpty() && !path.isEmpty());
359
 
        /* Searching root for the new listview item */
360
 
        SFTreeViewItem *root = searchRoot (isPermanent);
361
 
        Assert (root);
362
 
        /* Appending a new listview item to the root */
363
 
        QStringList fields;
364
 
        fields << name /* name */ << path /* path */
365
 
               << (dlg.isWriteable() ? mTrFull : mTrReadOnly /* writable? */)
366
 
               << "edited" /* mark item as edited */;
367
 
        SFTreeViewItem *item = new SFTreeViewItem (root, fields, SFTreeViewItem::EllipsisFile);
368
 
        mTwFolders->sortItems (0, Qt::AscendingOrder);
369
 
        mTwFolders->scrollToItem (item);
370
 
        mTwFolders->setCurrentItem (item);
371
 
        processCurrentChanged (item);
372
 
        mTwFolders->setFocus();
373
 
        adjustList();
374
 
 
375
 
        mIsListViewChanged = true;
376
 
    }
377
 
}
378
 
 
379
 
void VBoxVMSettingsSF::edtTriggered()
380
 
{
381
 
    /* Check selected item */
382
 
    QTreeWidgetItem *selectedItem = mTwFolders->selectedItems().size() == 1 ? mTwFolders->selectedItems() [0] : 0;
383
 
    SFTreeViewItem *item = selectedItem && selectedItem->type() == SFTreeViewItem::SFTreeViewItemType ?
384
 
                           static_cast <SFTreeViewItem*> (selectedItem) : 0;
385
 
    Assert (item);
386
 
    Assert (item->parent());
387
 
 
388
 
    /* Invoke Edit-Box Dialog */
389
 
    VBoxVMSettingsSFDetails dlg (VBoxVMSettingsSFDetails::EditType, mDialogType & ConsoleType, usedList (false), this);
390
 
    dlg.setPath (item->getText (1));
391
 
    dlg.setName (item->getText (0));
392
 
    dlg.setPermanent ((SFDialogType)item->parent()->text (1).toInt() != ConsoleType);
393
 
    dlg.setWriteable (item->getText (2) == mTrFull);
394
 
    if (dlg.exec() == QDialog::Accepted)
395
 
    {
396
 
        QString name = dlg.name();
397
 
        QString path = dlg.path();
398
 
        bool isPermanent = dlg.isPermanent();
399
 
        /* Shared folder's name & path could not be empty */
400
 
        Assert (!name.isEmpty() && !path.isEmpty());
401
 
        /* Searching new root for the selected listview item */
402
 
        SFTreeViewItem *root = searchRoot (isPermanent);
403
 
        Assert (root);
404
 
        /* Updating an edited listview item */
405
 
        QStringList fields;
406
 
        fields << name /* name */ << path /* path */
407
 
               << (dlg.isWriteable() ? mTrFull : mTrReadOnly /* writable? */)
408
 
               << "edited" /* mark item as edited */;
409
 
        item->updateText (fields);
410
 
        mTwFolders->sortItems (0, Qt::AscendingOrder);
411
 
        if (item->parent() != root)
412
 
        {
413
 
            /* Move the selected item into new location */
414
 
            item->parent()->takeChild (item->parent()->indexOfChild (item));
415
 
            root->insertChild (root->childCount(), item);
416
 
            mTwFolders->scrollToItem (item);
417
 
            mTwFolders->setCurrentItem (item);
418
 
            processCurrentChanged (item);
419
 
            mTwFolders->setFocus();
420
 
        }
421
 
        adjustList();
422
 
 
423
 
        mIsListViewChanged = true;
424
 
    }
425
 
}
426
 
 
427
 
void VBoxVMSettingsSF::delTriggered()
428
 
{
429
 
    QTreeWidgetItem *selectedItem = mTwFolders->selectedItems().size() == 1 ? mTwFolders->selectedItems() [0] : 0;
430
 
    Assert (selectedItem);
431
 
    delete selectedItem;
432
 
    adjustList();
433
 
    mIsListViewChanged = true;
434
 
}
435
 
 
436
 
void VBoxVMSettingsSF::processCurrentChanged (QTreeWidgetItem *aCurrentItem)
437
 
{
438
 
    if (aCurrentItem && aCurrentItem->parent() && !aCurrentItem->isSelected())
439
 
        aCurrentItem->setSelected (true);
440
 
    QString key = !aCurrentItem ? QString::null : aCurrentItem->parent() ?
441
 
                  aCurrentItem->parent()->text (1) : aCurrentItem->text (1);
442
 
    bool addEnabled = aCurrentItem && isEditable (key);
443
 
    bool removeEnabled = addEnabled && aCurrentItem->parent();
444
 
    mNewAction->setEnabled (addEnabled);
445
 
    mEdtAction->setEnabled (removeEnabled);
446
 
    mDelAction->setEnabled (removeEnabled);
447
 
}
448
 
 
449
 
void VBoxVMSettingsSF::processDoubleClick (QTreeWidgetItem *aItem)
450
 
{
451
 
    bool editEnabled = aItem && aItem->parent() && isEditable (aItem->parent()->text (1));
452
 
    if (editEnabled)
453
 
        edtTriggered();
454
 
}
455
 
 
456
 
void VBoxVMSettingsSF::showContextMenu (const QPoint &aPos)
457
 
{
458
 
    QMenu menu;
459
 
    QTreeWidgetItem *item = mTwFolders->itemAt (aPos);
460
 
    if (item && item->flags() & Qt::ItemIsSelectable)
461
 
    {
462
 
        menu.addAction (mEdtAction);
463
 
        menu.addAction (mDelAction);
464
 
    }
465
 
    else
466
 
    {
467
 
        menu.addAction (mNewAction);
468
 
    }
469
 
    menu.exec (mTwFolders->viewport()->mapToGlobal (aPos));
470
 
}
471
 
 
472
 
void VBoxVMSettingsSF::adjustList()
473
 
{
474
 
    /* Calculating required columns size & watching those columns (0 and 2) to feat 1/3 of total width. */
475
 
    QAbstractItemView *itemView = mTwFolders;
476
 
    QHeaderView *itemHeader = mTwFolders->header();
477
 
    int total = mTwFolders->viewport()->width();
478
 
    int mw0 = qMax (itemView->sizeHintForColumn (0), itemHeader->sectionSizeHint (0));
479
 
    int mw2 = qMax (itemView->sizeHintForColumn (2), itemHeader->sectionSizeHint (2));
480
 
    int w0 = mw0 < total / 3 ? mw0 : total / 3;
481
 
    int w2 = mw2 < total / 3 ? mw2 : total / 3;
482
 
 
483
 
    /* Giving 1st column all the available space. */
484
 
    mTwFolders->setColumnWidth (0, w0);
485
 
    mTwFolders->setColumnWidth (1, total - w0 - w2);
486
 
    mTwFolders->setColumnWidth (2, w2);
487
 
}
488
 
 
489
 
void VBoxVMSettingsSF::adjustFields()
490
 
{
491
 
    QTreeWidgetItem *mainRoot = mTwFolders->invisibleRootItem();
492
 
    for (int i = 0; i < mainRoot->childCount(); ++ i)
493
 
    {
494
 
        QTreeWidgetItem *subRoot = mainRoot->child (i);
495
 
        for (int j = 0; j < subRoot->childCount(); ++ j)
496
 
        {
497
 
            SFTreeViewItem *item = subRoot->child (j) &&
498
 
                                   subRoot->child (j)->type() == SFTreeViewItem::SFTreeViewItemType ?
499
 
                                   static_cast <SFTreeViewItem*> (subRoot->child (j)) : 0;
500
 
            if (item)
501
 
                item->adjustText();
502
 
        }
503
 
    }
504
 
}
505
 
 
506
 
void VBoxVMSettingsSF::showEvent (QShowEvent *aEvent)
507
 
{
508
 
    QWidget::showEvent (aEvent);
509
 
 
510
 
    /* Connect header-resize signal just before widget is shown after all the items properly loaded and initialized. */
511
 
    connect (mTwFolders->header(), SIGNAL (sectionResized (int, int, int)), this, SLOT (adjustFields()));
512
 
 
513
 
    /* Adjusting size after all pending show events are processed. */
514
 
    QTimer::singleShot (0, this, SLOT (adjustList()));
515
 
}
516
 
 
517
 
void VBoxVMSettingsSF::createSharedFolder (const QString &aName, const QString &aPath, bool aWritable, SFDialogType aType)
518
 
{
519
 
    switch (aType)
520
 
    {
521
 
        case GlobalType:
522
 
        {
523
 
            /* This feature is not supported now */
524
 
            AssertMsgFailed (("Global shared folders are not supported now!\n"));
525
 
            break;
526
 
        }
527
 
        case MachineType:
528
 
        {
529
 
            Assert (!mMachine.isNull());
530
 
            mMachine.CreateSharedFolder (aName, aPath, aWritable);
531
 
            if (!mMachine.isOk())
532
 
                vboxProblem().cannotCreateSharedFolder (this, mMachine, aName, aPath);
533
 
            break;
534
 
        }
535
 
        case ConsoleType:
536
 
        {
537
 
            Assert (!mConsole.isNull());
538
 
            mConsole.CreateSharedFolder (aName, aPath, aWritable);
539
 
            if (!mConsole.isOk())
540
 
                vboxProblem().cannotCreateSharedFolder (this, mConsole, aName, aPath);
541
 
            break;
542
 
        }
543
 
        default:
544
 
        {
545
 
            AssertMsgFailed (("Incorrect shared folder type\n"));
546
 
        }
547
 
    }
548
 
}
549
 
 
550
 
void VBoxVMSettingsSF::removeSharedFolder (const QString &aName, const QString &aPath, SFDialogType aType)
551
 
{
552
 
    switch (aType)
553
 
    {
554
 
        case GlobalType:
555
 
        {
556
 
            /* This feature is not supported now */
557
 
            AssertMsgFailed (("Global shared folders are not supported now!\n"));
558
 
            break;
559
 
        }
560
 
        case MachineType:
561
 
        {
562
 
            Assert (!mMachine.isNull());
563
 
            mMachine.RemoveSharedFolder (aName);
564
 
            if (!mMachine.isOk())
565
 
                vboxProblem().cannotRemoveSharedFolder (this, mMachine, aName, aPath);
566
 
            break;
567
 
        }
568
 
        case ConsoleType:
569
 
        {
570
 
            Assert (!mConsole.isNull());
571
 
            mConsole.RemoveSharedFolder (aName);
572
 
            if (!mConsole.isOk())
573
 
                vboxProblem().cannotRemoveSharedFolder (this, mConsole, aName, aPath);
574
 
            break;
575
 
        }
576
 
        default:
577
 
        {
578
 
            AssertMsgFailed (("Incorrect shared folder type\n"));
579
 
        }
580
 
    }
581
 
}
582
 
 
583
 
void VBoxVMSettingsSF::getFrom (const CSharedFolderVector &aVec, SFTreeViewItem *aRoot)
584
 
{
585
 
    for (int i = 0; i < aVec.size(); ++ i)
586
 
    {
587
 
        CSharedFolder sf = aVec [i];
588
 
        QStringList fields;
589
 
        fields << sf.GetName() /* name */ << sf.GetHostPath() /* path */
590
 
               << (sf.GetWritable() ? mTrFull : mTrReadOnly /* writable? */)
591
 
               << "not edited" /* initially not edited */;
592
 
        new SFTreeViewItem (aRoot, fields, SFTreeViewItem::EllipsisFile);
593
 
    }
594
 
    aRoot->setExpanded (true);
595
 
    mTwFolders->sortItems (0, Qt::AscendingOrder);
596
 
    mTwFolders->setCurrentItem (aRoot->childCount() ? aRoot->child (0) : aRoot);
597
 
    processCurrentChanged (aRoot->childCount() ? aRoot->child (0) : aRoot);
598
 
}
599
 
 
600
 
void VBoxVMSettingsSF::putBackTo (CSharedFolderVector &aVec, SFTreeViewItem *aRoot)
601
 
{
602
 
    Assert (!aRoot->text (1).isNull());
603
 
    SFDialogType type = (SFDialogType) aRoot->text (1).toInt();
604
 
 
605
 
    /* Delete all changed folders from vm */
606
 
    for (int idx = 0; idx < aVec.size(); ++ idx)
607
 
    {
608
 
        CSharedFolder sf = aVec [idx];
609
 
 
610
 
        /* Iterate through this root's children */
611
 
        int i = 0;
612
 
        for (; i < aRoot->childCount(); ++ i)
613
 
        {
614
 
            SFTreeViewItem *item = aRoot->child (i);
615
 
            if (item->getText (0) == sf.GetName() && item->getText (3) == "not edited")
616
 
                break;
617
 
        }
618
 
 
619
 
        if (i == aRoot->childCount())
620
 
            removeSharedFolder (sf.GetName(), sf.GetHostPath(), type);
621
 
    }
622
 
 
623
 
    /* Save all edited tree widget items as folders */
624
 
    for (int i = 0; i < aRoot->childCount(); ++ i)
625
 
    {
626
 
        SFTreeViewItem *item = aRoot->child (i);
627
 
 
628
 
        if (!item->getText (0).isNull() && !item->getText (1).isNull() && item->getText (3) == "edited")
629
 
            createSharedFolder (item->getText (0), item->getText (1), item->getText (2) == mTrFull ? true : false, type);
630
 
    }
631
 
}
632
 
 
633
 
SFTreeViewItem* VBoxVMSettingsSF::searchRoot (bool aIsPermanent, SFDialogType aType)
634
 
{
635
 
    QString type = aType != WrongType ? QString::number (aType) : !aIsPermanent ? QString::number (ConsoleType) :
636
 
                   mDialogType & MachineType ? QString::number (MachineType) : QString::number (GlobalType);
637
 
    QTreeWidgetItem *mainRoot = mTwFolders->invisibleRootItem();
638
 
 
639
 
    int i = 0;
640
 
    for (; i < mainRoot->childCount(); ++ i)
641
 
    {
642
 
        if (mainRoot->child (i)->text (1) == type)
643
 
            break;
644
 
    }
645
 
 
646
 
    Assert (i < mainRoot->childCount());
647
 
    return i < mainRoot->childCount() && mainRoot->child (i)->type() == SFTreeViewItem::SFTreeViewItemType ?
648
 
           static_cast <SFTreeViewItem*> (mainRoot->child (i)) : 0;
649
 
}
650
 
 
651
 
bool VBoxVMSettingsSF::isEditable (const QString &aKey)
652
 
{
653
 
    /* mDialogType should be correct */
654
 
    Assert (mDialogType);
655
 
 
656
 
    SFDialogType type = (SFDialogType) aKey.toInt();
657
 
    if (!type) return false;
658
 
    return mDialogType & type;
659
 
}
660
 
 
661
 
SFoldersNameList VBoxVMSettingsSF::usedList (bool aIncludeSelected)
662
 
{
663
 
    /* Make the used names list: */
664
 
    SFoldersNameList list;
665
 
    QTreeWidgetItemIterator it (mTwFolders);
666
 
    while (*it)
667
 
    {
668
 
        if ((*it)->parent() && (aIncludeSelected || !(*it)->isSelected()) &&
669
 
            (*it)->type() == SFTreeViewItem::SFTreeViewItemType)
670
 
        {
671
 
            SFTreeViewItem *item = static_cast <SFTreeViewItem*> (*it);
672
 
            SFDialogType type = (SFDialogType) item->parent()->text (1).toInt();
673
 
            list << qMakePair (item->getText (0), type);
674
 
        }
675
 
        ++ it;
676
 
    }
677
 
    return list;
678
 
}
679