VirtualBox

Changeset 16218 in vbox


Ignore:
Timestamp:
Jan 26, 2009 10:16:38 AM (16 years ago)
Author:
vboxsync
Message:

OVF: Added VirtualBox object member, make the vm name unique & use them on import.

Location:
trunk/src/VBox/Main
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Main/ApplianceImpl.cpp

    r16212 r16218  
    185185
    186186/**
    187  * Implementation for IAppliance::openAppliance. Loads the given appliance (see API reference).
     187 * Implementation for IVirtualBox::openAppliance. Loads the given appliance (see API reference).
    188188 *
    189189 * @param bstrPath Appliance to open (either .ovf or .ova file, see API reference)
     
    191191 * @return S_OK or error.
    192192 */
    193 STDMETHODIMP VirtualBox::OpenAppliance(IN_BSTR bstrPath, IAppliance** anAppliance)
     193STDMETHODIMP VirtualBox::OpenAppliance (IN_BSTR bstrPath, IAppliance** anAppliance)
    194194{
    195195    HRESULT rc;
     
    197197    ComObjPtr<Appliance> appliance;
    198198    appliance.createObject();
    199     rc = appliance->init(bstrPath);
     199    rc = appliance->init (this, bstrPath);
    200200//     ComAssertComRCThrowRC(rc);
    201201
    202202    if (SUCCEEDED(rc))
    203     {
    204203        appliance.queryInterfaceTo(anAppliance);
    205     }
    206204
    207205    return rc;
     
    715713 */
    716714
    717 HRESULT Appliance::init(IN_BSTR &path)
     715HRESULT Appliance::init (VirtualBox *aVirtualBox, IN_BSTR &path)
    718716{
    719717    HRESULT rc;
     
    722720    AutoInitSpan autoInitSpan(this);
    723721    AssertReturn (autoInitSpan.isOk(), E_FAIL);
     722
     723    /* Weakly reference to a VirtualBox object */
     724    unconst (mVirtualBox) = aVirtualBox;
    724725
    725726    // initialize data
     
    875876STDMETHODIMP Appliance::ImportAppliance()
    876877{
    877     /* We need a virtualbox object */
    878     ComPtr <IVirtualBox> virtualBox;
    879     HRESULT rc = virtualBox.createLocalObject (CLSID_VirtualBox);
    880     ComAssertComRCThrowRC (rc);
     878    HRESULT rc = S_OK;
    881879
    882880    list<VirtualSystem>::const_iterator it;
     
    899897        /* Now that we know the base system get our internal defaults based on that. */
    900898        IGuestOSType *osType = NULL;
    901         rc = virtualBox->GetGuestOSType (Bstr (Utf8Str (osTypeVBox.c_str())), &osType);
     899        rc = mVirtualBox->GetGuestOSType (Bstr (Utf8Str (osTypeVBox.c_str())), &osType);
    902900        ComAssertComRCThrowRC (rc);
    903901
    904902        /* Create the machine */
     903        /* First get the name */
     904        list<VirtualSystemDescriptionEntry> vsdeName = vsd->findByType (VirtualSystemDescriptionType_Name);
     905        Assert (vsdeName.size() == 1);
     906        string nameVBox = vsdeName.front().strFinalValue;
    905907        IMachine *newMachine = NULL;
    906         rc = virtualBox->CreateMachine (Bstr (Utf8StrFmt ("tescht_%d", i)), Bstr (Utf8Str (osTypeVBox.c_str())),
    907                                         Bstr (), Guid(),
    908                                         &newMachine);
     908        rc = mVirtualBox->CreateMachine (Bstr (nameVBox.c_str()), Bstr (osTypeVBox.c_str()),
     909                                         Bstr (), Guid(),
     910                                         &newMachine);
    909911        ComAssertComRCThrowRC (rc);
    910912
     
    963965        }
    964966        /* Now its time to register the machine before we add any hard disks */
    965         rc = virtualBox->RegisterMachine (newMachine);
     967        rc = mVirtualBox->RegisterMachine (newMachine);
    966968        ComAssertComRCThrowRC (rc);
    967969
     
    986988    //  - Appropriate handle errors like not supported file formats
    987989
     990    HRESULT rc = S_OK;
     991
    988992    /* Clear any previous virtual system descriptions */
    989993    // @todo: have the entries deleted also?
    990994    m->virtualSystemDescriptions.clear();
    991995
    992     /* We need a virtualbox object */
    993     ComPtr <IVirtualBox> virtualBox;
    994     HRESULT rc = virtualBox.createLocalObject (CLSID_VirtualBox);
    995     ComAssertComRCThrowRC (rc);
    996 
    997996    /* We need the default path for storing disk images */
    998997    ISystemProperties *systemProps = NULL;
    999     rc = virtualBox->COMGETTER(SystemProperties) (&systemProps);
     998    rc = mVirtualBox->COMGETTER(SystemProperties) (&systemProps);
    1000999    ComAssertComRCThrowRC (rc);
    10011000    BSTR defaultHardDiskLocation;
     
    10151014        ComAssertComRCThrowRC(rc);
    10161015
    1017         /* VM name */
    1018         vsd->addEntry(VirtualSystemDescriptionType_Name, 0, vs.strName, vs.strName);
    1019 
    10201016        string osTypeVBox = SchemaDefs_OSTypeId_Other;
    10211017        /* Guest OS type */
     
    12041200        vsd->addEntry (VirtualSystemDescriptionType_OS, 0, toString<ULONG> (vs.cimos), osTypeVBox);
    12051201
     1202        /* VM name */
     1203        /* If the there isn't any name specified create a default one out of
     1204         * the OS type */
     1205        string nameVBox = vs.strName;
     1206        if (nameVBox == "")
     1207            nameVBox = osTypeVBox;
     1208        /* @todo: make sure the name is unique (add some numbers if not) */
     1209        searchUniqueVMName (nameVBox);
     1210        vsd->addEntry(VirtualSystemDescriptionType_Name, 0, nameVBox, nameVBox);
     1211
    12061212        /* Now that we know the base system get our internal defaults based on that. */
    12071213        IGuestOSType *osType = NULL;
    1208         rc = virtualBox->GetGuestOSType (Bstr (Utf8Str (osTypeVBox.c_str())), &osType);
     1214        rc = mVirtualBox->GetGuestOSType (Bstr (Utf8Str (osTypeVBox.c_str())), &osType);
    12091215        ComAssertComRCThrowRC (rc);
    12101216
     
    13301336}
    13311337
     1338HRESULT Appliance::searchUniqueVMName (std::string& aName)
     1339{
     1340    IMachine *machine = NULL;
     1341    char *tmpName = RTStrDup (aName.c_str());
     1342    int i = 1;
     1343    /* @todo: Maybe to cost intensive; try to find a lighter way */
     1344    while (mVirtualBox->FindMachine (Bstr (tmpName), &machine) != VBOX_E_OBJECT_NOT_FOUND)
     1345    {
     1346        RTStrFree (tmpName);
     1347        RTStrAPrintf (&tmpName, "%s_%d", aName.c_str(), i);
     1348        ++i;
     1349    }
     1350    aName = tmpName;
     1351    RTStrFree (tmpName);
     1352
     1353    return S_OK;
     1354}
     1355
    13321356// IVirtualSystemDescription constructor / destructor
    13331357////////////////////////////////////////////////////////////////////////////////
  • trunk/src/VBox/Main/include/ApplianceImpl.h

    r16205 r16218  
    2626
    2727#include "VirtualBoxBase.h"
     28
     29class VirtualBox;
    2830
    2931class ATL_NO_VTABLE Appliance :
     
    5557    void FinalRelease() { uninit(); }
    5658
    57     HRESULT init(IN_BSTR &path);
     59    HRESULT init (VirtualBox *aVirtualBox, IN_BSTR &path);
    5860    void uninit();
    5961
     
    7577    /* private instance data */
    7678private:
     79    /** weak VirtualBox parent */
     80    const ComObjPtr <VirtualBox, ComWeakRef> mVirtualBox;
     81
    7782    struct Data;            // obscure, defined in AppliannceImpl.cpp
    7883    Data *m;
     
    8489
    8590    HRESULT construeAppliance();
     91    HRESULT searchUniqueVMName (std::string& aName);
    8692};
    8793
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