VirtualBox

Changeset 45452 in vbox


Ignore:
Timestamp:
Apr 10, 2013 10:19:38 AM (12 years ago)
Author:
vboxsync
Message:

FE/Qt: Well predefined create/destroy points for VBoxGlobal, Modal-window manager, Message-center and Popup-center.

Location:
trunk/src/VBox/Frontends/VirtualBox/src
Files:
9 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Frontends/VirtualBox/src/globals/UIMessageCenter.cpp

    r45432 r45452  
    7070#include <iprt/param.h>
    7171#include <iprt/path.h>
     72
     73/* static */
     74UIMessageCenter* UIMessageCenter::m_spInstance = 0;
     75UIMessageCenter* UIMessageCenter::instance() { return m_spInstance; }
     76
     77/* static */
     78void UIMessageCenter::create()
     79{
     80    /* Make sure instance is NOT created yet: */
     81    if (m_spInstance)
     82    {
     83        AssertMsgFailed(("UIMessageCenter instance is already created!"));
     84        return;
     85    }
     86
     87    /* Create instance: */
     88    new UIMessageCenter;
     89    /* Prepare instance: */
     90    m_spInstance->prepare();
     91}
     92
     93/* static */
     94void UIMessageCenter::destroy()
     95{
     96    /* Make sure instance is NOT destroyed yet: */
     97    if (!m_spInstance)
     98    {
     99        AssertMsgFailed(("UIMessageCenter instance is already destroyed!"));
     100        return;
     101    }
     102
     103    /* Cleanup instance: */
     104    m_spInstance->cleanup();
     105    /* Destroy instance: */
     106    delete m_spInstance;
     107}
    72108
    73109bool UIMessageCenter::warningShown(const QString &strWarningName) const
     
    25342570UIMessageCenter::UIMessageCenter()
    25352571{
     2572    /* Assign instance: */
     2573    m_spInstance = this;
     2574}
     2575
     2576UIMessageCenter::~UIMessageCenter()
     2577{
     2578    /* Unassign instance: */
     2579    m_spInstance = 0;
     2580}
     2581
     2582void UIMessageCenter::prepare()
     2583{
    25362584    /* Register required objects as meta-types: */
    25372585    qRegisterMetaType<CProgress>();
     
    25702618}
    25712619
    2572 /* Returns a reference to the global VirtualBox message center instance: */
    2573 UIMessageCenter &UIMessageCenter::instance()
    2574 {
    2575     static UIMessageCenter global_instance;
    2576     return global_instance;
     2620void UIMessageCenter::cleanup()
     2621{
     2622     /* Nothing for now... */
    25772623}
    25782624
  • trunk/src/VBox/Frontends/VirtualBox/src/globals/UIMessageCenter.h

    r45432 r45452  
    7373        AutoConfirmed = 0x8000
    7474    };
     75
     76    /* Static API: Create/destroy stuff: */
     77    static void create();
     78    static void destroy();
    7579
    7680    /* API: Warning registration stuff: */
     
    383387private:
    384388
    385     /* Constructor: */
     389    /* Constructor/destructor: */
    386390    UIMessageCenter();
    387 
    388     /* Instance stuff: */
    389     static UIMessageCenter &instance();
    390     friend UIMessageCenter &msgCenter();
     391    ~UIMessageCenter();
     392
     393    /* Helpers: Prepare/cleanup stuff: */
     394    void prepare();
     395    void cleanup();
    391396
    392397    /* Helper: */
     
    402407    /* Variables: */
    403408    mutable QStringList m_warnings;
     409
     410    /* API: Instance stuff: */
     411    static UIMessageCenter* m_spInstance;
     412    static UIMessageCenter* instance();
     413    friend UIMessageCenter& msgCenter();
    404414};
    405415
    406 /* Shortcut to the static UIMessageCenter::instance() method, for convenience. */
    407 inline UIMessageCenter &msgCenter() { return UIMessageCenter::instance(); }
     416/* Shortcut to the static UIMessageCenter::instance() method: */
     417inline UIMessageCenter& msgCenter() { return *UIMessageCenter::instance(); }
    408418
    409419#endif // __UIMessageCenter_h__
  • trunk/src/VBox/Frontends/VirtualBox/src/globals/UIModalWindowManager.cpp

    r45441 r45452  
    2929
    3030/* static */
     31UIModalWindowManager* UIModalWindowManager::m_spInstance = 0;
    3132UIModalWindowManager* UIModalWindowManager::instance() { return m_spInstance; }
    32 UIModalWindowManager* UIModalWindowManager::m_spInstance = 0;
    3333
    3434/* static */
    3535void UIModalWindowManager::create()
    3636{
    37     /* Make sure instance is not created: */
     37    /* Make sure instance is NOT created yet: */
    3838    if (m_spInstance)
    39         return;
     39    {
     40        AssertMsgFailed(("UIModalWindowManager instance is already created!"));
     41        return;
     42    }
    4043
    4144    /* Create instance: */
     
    4649void UIModalWindowManager::destroy()
    4750{
    48     /* Make sure instance is created: */
     51    /* Make sure instance is NOT destroyed yet: */
    4952    if (!m_spInstance)
    50         return;
    51 
    52     /* Create instance: */
     53    {
     54        AssertMsgFailed(("UIModalWindowManager instance is already destroyed!"));
     55        return;
     56    }
     57
     58    /* Destroy instance: */
    5359    delete m_spInstance;
    5460}
     
    5662UIModalWindowManager::UIModalWindowManager()
    5763{
    58     /* Make sure instance is not assigned: */
    59     if (m_spInstance != this)
    60         m_spInstance = this;
     64    /* Assign instance: */
     65    m_spInstance = this;
    6166}
    6267
    6368UIModalWindowManager::~UIModalWindowManager()
    6469{
    65     /* Make sure instance still assigned: */
    66     if (m_spInstance == this)
    67         m_spInstance = 0;
     70    /* Unassign instance: */
     71    m_spInstance = 0;
    6872}
    6973
    7074QWidget* UIModalWindowManager::mainWindowShown() const
    7175{
    72     /* Later this function will be independent of VBoxGlobal at all,
    73      * but for now VBoxGlobal creates all the main application windows,
    74      * so we should honor this fact.
    75      *
    76      * It may happen that this method is called during VBoxGlobal
    77      * initialization or even after it had failed (for example, to show some message).
    78      * Return NULL pointer in this case: */
    79     if (!vboxGlobal().isValid())
     76    /* It may happen that this method is called before VBoxGlobal initialization
     77     * or after initialization had failed (for example, to show some message).
     78     * Return NULL pointer in such cases: */
     79    if (!VBoxGlobal::instance() || !vboxGlobal().isValid())
    8080        return 0;
    8181
     
    8888            return vboxGlobal().activeMachineWindow();
    8989    }
    90     /* Otherwise: */
     90    /* For VM selector process: */
    9191    else
    9292    {
     
    9696    }
    9797
    98     /* Nothing by default: */
     98    /* NULL by default: */
    9999    return 0;
    100100}
  • trunk/src/VBox/Frontends/VirtualBox/src/globals/UIModalWindowManager.h

    r45432 r45452  
    22 *
    33 * VBox frontends: Qt GUI ("VirtualBox"):
    4  * UIModalWindowManager class definition
     4 * UIModalWindowManager class declaration
    55 */
    66
     
    6666    /* Variables: */
    6767    QList<QList<QWidget*> > m_windows;
    68     static UIModalWindowManager* m_spInstance;
    6968
    7069    /* Static API: Instance stuff: */
     70    static UIModalWindowManager* m_spInstance;
    7171    static UIModalWindowManager* instance();
    7272    friend UIModalWindowManager& windowManager();
  • trunk/src/VBox/Frontends/VirtualBox/src/globals/UIPopupCenter.cpp

    r45431 r45452  
    3232
    3333/* static */
    34 void UIPopupCenter::prepare()
     34void UIPopupCenter::create()
    3535{
    36     /* Make sure instance is not created yet: */
     36    /* Make sure instance is NOT created yet: */
    3737    if (m_spInstance)
    3838        return;
     
    4343
    4444/* static */
    45 void UIPopupCenter::cleanup()
     45void UIPopupCenter::destroy()
    4646{
    47     /* Make sure instance is still created: */
     47    /* Make sure instance is NOT destroyed yet: */
    4848    if (!m_spInstance)
    4949        return;
    5050
    51     /* Create instance: */
     51    /* Destroy instance: */
    5252    delete m_spInstance;
    5353}
     
    5555UIPopupCenter::UIPopupCenter()
    5656{
    57     /* Prepare instance: */
    58     if (!m_spInstance)
    59         m_spInstance = this;
     57    /* Assign instance: */
     58    m_spInstance = this;
    6059}
    6160
    6261UIPopupCenter::~UIPopupCenter()
    6362{
    64     /* Cleanup instance: */
    65     if (m_spInstance)
    66         m_spInstance = 0;
     63    /* Unassign instance: */
     64    m_spInstance = 0;
    6765}
    6866
  • trunk/src/VBox/Frontends/VirtualBox/src/globals/UIPopupCenter.h

    r45431 r45452  
    3232public:
    3333
    34     /* Prepare/cleanup stuff: */
    35     static void prepare();
    36     static void cleanup();
     34    /* Static API: Create/destroy stuff: */
     35    static void create();
     36    static void destroy();
    3737
    3838    /* API: Main message function, used directly only in exceptional cases: */
     
    9090    ~UIPopupCenter();
    9191
    92     /* Instance stuff: */
    93     static UIPopupCenter* instance();
    94     friend UIPopupCenter& popupCenter();
    95 
    9692    /* Helper: Popup-box stuff: */
    9793    void showPopupBox(QWidget *pParent,
     
    10298
    10399    /* Variables: */
     100    mutable QMap<QString, QPointer<QWidget> > m_popups;
     101
     102    /* Instance stuff: */
    104103    static UIPopupCenter* m_spInstance;
    105     mutable QMap<QString, QPointer<QWidget> > m_popups;
     104    static UIPopupCenter* instance();
     105    friend UIPopupCenter& popupCenter();
    106106};
    107107
    108 /* Shortcut to the static UIPopupCenter::instance() method, for convenience: */
     108/* Shortcut to the static UIPopupCenter::instance() method: */
    109109inline UIPopupCenter& popupCenter() { return *UIPopupCenter::instance(); }
    110110
  • trunk/src/VBox/Frontends/VirtualBox/src/globals/VBoxGlobal.cpp

    r45431 r45452  
    186186// VBoxGlobal
    187187////////////////////////////////////////////////////////////////////////////////
    188 
    189 static bool sVBoxGlobalInited = false;
    190 static bool sVBoxGlobalInCleanup = false;
    191 
    192 /** @internal
    193  *
    194  *  Special routine to do VBoxGlobal cleanup when the application is being
    195  *  terminated. It is called before some essential Qt functionality (for
    196  *  instance, QThread) becomes unavailable, allowing us to use it from
    197  *  VBoxGlobal::cleanup() if necessary.
    198  */
    199 static void vboxGlobalCleanup()
    200 {
    201     Assert (!sVBoxGlobalInCleanup);
    202     sVBoxGlobalInCleanup = true;
    203     vboxGlobal().cleanup();
    204 }
    205188
    206189/** @internal
     
    258241}
    259242
    260 /** @class VBoxGlobal
    261  *
    262  *  The VBoxGlobal class encapsulates the global VirtualBox data.
    263  *
    264  *  There is only one instance of this class per VirtualBox application,
    265  *  the reference to it is returned by the static instance() method, or by
    266  *  the global vboxGlobal() function, that is just an inlined shortcut.
    267  */
     243/* static */
     244bool VBoxGlobal::m_sfCleanupInProgress = false;
     245VBoxGlobal* VBoxGlobal::m_spInstance = 0;
     246VBoxGlobal* VBoxGlobal::instance() { return m_spInstance; }
     247
     248/* static */
     249void VBoxGlobal::create()
     250{
     251    /* Make sure instance is NOT created yet: */
     252    if (m_spInstance)
     253    {
     254        AssertMsgFailed(("VBoxGlobal instance is already created!"));
     255        return;
     256    }
     257
     258    /* Create instance: */
     259    new VBoxGlobal;
     260    /* Prepare instance: */
     261    m_spInstance->prepare();
     262}
     263
     264/* static */
     265void VBoxGlobal::destroy()
     266{
     267    /* Make sure instance is NOT destroyed yet: */
     268    if (!m_spInstance)
     269    {
     270        AssertMsgFailed(("VBoxGlobal instance is already destroyed!"));
     271        return;
     272    }
     273
     274    /* Cleanup instance: */
     275    /* Automatically on QApplication::aboutToQuit() signal: */
     276    /* Destroy instance: */
     277    delete m_spInstance;
     278}
    268279
    269280VBoxGlobal::VBoxGlobal()
     
    282293    , mSettingsPwSet(false)
    283294{
    284 }
    285 
    286 //
    287 // Public members
    288 /////////////////////////////////////////////////////////////////////////////
    289 
    290 /**
    291  *  Returns a reference to the global VirtualBox data, managed by this class.
    292  *
    293  *  The main() function of the VBox GUI must call this function soon after
    294  *  creating a QApplication instance but before opening any of the main windows
    295  *  (to let the VBoxGlobal initialization procedure use various Qt facilities),
    296  *  and continue execution only when the isValid() method of the returned
    297  *  instancereturns true, i.e. do something like:
    298  *
    299  *  @code
    300  *  if ( !VBoxGlobal::instance().isValid() )
    301  *      return 1;
    302  *  @endcode
    303  *  or
    304  *  @code
    305  *  if ( !vboxGlobal().isValid() )
    306  *      return 1;
    307  *  @endcode
    308  *
    309  *  @note Some VBoxGlobal methods can be used on a partially constructed
    310  *  VBoxGlobal instance, i.e. from constructors and methods called
    311  *  from the VBoxGlobal::init() method, which obtain the instance
    312  *  using this instance() call or the ::vboxGlobal() function. Currently, such
    313  *  methods are:
    314  *      #vmStateText, #vmTypeIcon, #vmTypeText, #vmTypeTextList, #vmTypeFromText.
    315  *
    316  *  @see ::vboxGlobal
    317  */
    318 VBoxGlobal &VBoxGlobal::instance()
    319 {
    320     static VBoxGlobal vboxGlobal_instance;
    321 
    322     if (!sVBoxGlobalInited)
    323     {
    324         /* check that a QApplication instance is created */
    325         if (qApp)
    326         {
    327             sVBoxGlobalInited = true;
    328             vboxGlobal_instance.init();
    329             /* add our cleanup handler to the list of Qt post routines */
    330             qAddPostRoutine (vboxGlobalCleanup);
    331         }
    332         else
    333             AssertMsgFailed (("Must construct a QApplication first!"));
    334     }
    335     return vboxGlobal_instance;
     295    /* Assign instance: */
     296    m_spInstance = this;
    336297}
    337298
    338299VBoxGlobal::~VBoxGlobal()
    339300{
    340     qDeleteAll (mOsTypeIcons);
     301    /* Unassign instance: */
     302    m_spInstance = 0;
    341303}
    342304
     
    17801742        return;
    17811743
    1782     /* ignore the request during application termination */
    1783     if (sVBoxGlobalInCleanup)
     1744    /* Ignore the request during VBoxGlobal cleanup: */
     1745    if (m_sfCleanupInProgress)
    17841746        return;
    17851747
     
    18171779
    18181780            /* Enumerate the list */
    1819             for (int i = 0; i < mVector.size() && !sVBoxGlobalInCleanup; ++ i)
     1781            for (int i = 0; i < mVector.size() && !m_sfCleanupInProgress; ++ i)
    18201782            {
    18211783                mVector [i].blockAndQueryState();
     
    18271789
    18281790            /* Post the end-of-enumeration event */
    1829             if (!sVBoxGlobalInCleanup)
     1791            if (!m_sfCleanupInProgress)
    18301792                QApplication::postEvent (self, new VBoxMediaEnumEvent (mSavedIt));
    18311793
     
    40193981}
    40203982
    4021 void VBoxGlobal::init()
    4022 {
     3983void VBoxGlobal::prepare()
     3984{
     3985    /* Make sure QApplication cleanup us on exit: */
     3986    connect(qApp, SIGNAL(aboutToQuit()), this, SLOT(cleanup()));
     3987
     3988    /* Create message-center: */
     3989    UIMessageCenter::create();
    40233990    /* Create popup-center: */
    4024     UIPopupCenter::prepare();
     3991    UIPopupCenter::create();
     3992
     3993    /* Load translation based on the current locale: */
     3994    loadLanguage();
    40253995
    40263996#ifdef DEBUG
     
    40684038    }
    40694039
    4070     /* Load the customized language as early as possible to get possible error
    4071      * messages translated */
     4040    /* Load translation based on the user settings: */
    40724041    QString sLanguageId = gset.languageId();
    40734042    if (!sLanguageId.isNull())
     
    44604429}
    44614430
    4462 
    4463 /** @internal
    4464  *
    4465  *  This method should be never called directly. It is called automatically
    4466  *  when the application terminates.
    4467  */
    44684431void VBoxGlobal::cleanup()
    44694432{
     4433    /* Preventing some unwanted stuff
     4434     * which could de called from the other threads: */
     4435    m_sfCleanupInProgress = true;
     4436
    44704437    /* Shutdown update manager: */
    44714438    UIUpdateManager::shutdown();
     
    44794446    /* Destroy shortcut pool: */
    44804447    UIShortcutPool::destroy();
    4481 
    4482     /* sanity check */
    4483     if (!sVBoxGlobalInCleanup)
    4484     {
    4485         AssertMsgFailed (("Should never be called directly\n"));
    4486         return;
    4487     }
    44884448
    44894449#ifdef VBOX_GUI_WITH_PIDFILE
     
    44974457    if (mMediaEnumThread)
    44984458    {
    4499         /* sVBoxGlobalInCleanup is true here, so just wait for the thread */
    45004459        mMediaEnumThread->wait();
    45014460        delete mMediaEnumThread;
     
    45094468
    45104469    UIConverter::cleanup();
     4470
     4471    /* Ensure mOsTypeIcons is cleaned up: */
     4472    qDeleteAll(mOsTypeIcons);
    45114473
    45124474    /* ensure CGuestOSType objects are no longer used */
     
    45284490
    45294491    /* Destroy popup-center: */
    4530     UIPopupCenter::cleanup();
     4492    UIPopupCenter::destroy();
     4493    /* Destroy message-center: */
     4494    UIMessageCenter::destroy();
    45314495
    45324496    mValid = false;
  • trunk/src/VBox/Frontends/VirtualBox/src/globals/VBoxGlobal.h

    r45402 r45452  
    6464public:
    6565
    66     static VBoxGlobal &instance();
     66    /* Static API: Create/destroy stuff: */
     67    static VBoxGlobal* instance();
     68    static void create();
     69    static void destroy();
    6770
    6871    bool isValid() { return mValid; }
     
    284287    void retranslateUi();
    285288
    286     /** @internal made public for internal purposes */
    287     void cleanup();
    288 
    289289    /* public static stuff */
    290290
     
    422422    void sltProcessGlobalSettingChange();
    423423
     424protected slots:
     425
     426    /* Handlers: Prepare/cleanup stuff: */
     427    void prepare();
     428    void cleanup();
     429
    424430protected:
    425431
     
    429435private:
    430436
     437    /* Constructor/destructor: */
    431438    VBoxGlobal();
    432439    ~VBoxGlobal();
    433440
    434     void init();
    435441#ifdef VBOX_WITH_DEBUGGER_GUI
    436442    void initDebuggerVar(int *piDbgCfgVar, const char *pszEnvVar, const char *pszExtraDataName, bool fDefault = false);
     
    522528    bool mSettingsPwSet;
    523529
    524     friend VBoxGlobal &vboxGlobal();
     530    /* API: Instance stuff: */
     531    static bool m_sfCleanupInProgress;
     532    static VBoxGlobal* m_spInstance;
     533    friend VBoxGlobal& vboxGlobal();
    525534};
    526535
    527 inline VBoxGlobal &vboxGlobal() { return VBoxGlobal::instance(); }
     536/* Shortcut to the static VBoxGlobal::instance() method: */
     537inline VBoxGlobal& vboxGlobal() { return *VBoxGlobal::instance(); }
    528538
    529539#endif /* __VBoxGlobal_h__ */
  • trunk/src/VBox/Frontends/VirtualBox/src/main.cpp

    r45448 r45452  
    412412        bool isCurrentScaleable = fontDataBase.isScalable(currentFamily);
    413413
    414         QString subFamily (QFont::substitute (currentFamily));
    415         bool isSubScaleable = fontDataBase.isScalable (subFamily);
     414        QString subFamily(QFont::substitute(currentFamily));
     415        bool isSubScaleable = fontDataBase.isScalable(subFamily);
    416416
    417417        if (isCurrentScaleable && !isSubScaleable)
    418             QFont::removeSubstitution (currentFamily);
     418            QFont::removeSubstitution(currentFamily);
    419419# endif /* Q_OS_SOLARIS */
    420420#endif /* Q_WS_X11 */
     
    446446                                  strMsg, QMessageBox::Abort, 0);
    447447            qFatal("%s", strMsg.toAscii().constData());
     448            break;
    448449        }
    449450#endif /* Q_WS_X11 */
     
    452453        UIModalWindowManager::create();
    453454
    454         /* Load a translation based on the current locale: */
    455         VBoxGlobal::loadLanguage();
     455        /* Create global UI instance: */
     456        VBoxGlobal::create();
    456457
    457458        /* Simulate try-catch block: */
     
    519520        while (0);
    520521
     522        /* Destroy global UI instance: */
     523        VBoxGlobal::destroy();
     524
    521525        /* Destroy modal-window manager: */
    522526        UIModalWindowManager::destroy();
Note: See TracChangeset for help on using the changeset viewer.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette