Exemple #1
0
bool PerformConfigMigration()
{
  /**
   *
   * We're here after the application's started and the conditions
   * listed above (in OfferConfigMigration) hold.
   * This constrains what we can assume and what we have to check.
   */

  ASSERT(OfferConfigMigration()); // should not be here otherwise!
  if (!OfferConfigMigration()) return false; // I mean it!

  PWSprefs::ConfigOption configoption;  // Note value meaningless at this point!
  std::wstring wsCnfgFile = PWSprefs::GetConfigFile(configoption);
  const std::wstring wsExecDir = pws_os::getexecdir();
  const std::wstring wsUserCfgDir = pws_os::getuserprefsdir();

  if (wsUserCfgDir.empty()) // couldn't find or create !?
    return false;

  std::wstring wsDefaultCfgFile = wsUserCfgDir + PWSprefs::cfgFileName;
  std::wstring wsExecDirCfgFile = wsExecDir + PWSprefs::cfgFileName;
  bool bRetVal(false);
  bool bExecCFRO(false);
  pws_os::FileExists(wsExecDirCfgFile, bExecCFRO);

  /**
   *  MIGRATE
  **/

  bRetVal = false;
  bool bNoMoreNodes(false);
  CXMLprefs newXMLConfig(wsExecDirCfgFile.c_str()); // for migrating user/host to new
  CXMLprefs oldXMLConfig(wsExecDirCfgFile.c_str()); // for removing user/host from old

  // Create the new one from it just containing our host/user
  if (!newXMLConfig.XML_Load())
    return false; // WTF?!?

  const SysInfo *si = SysInfo::GetInstance();
  stringT hn = si->GetEffectiveHost();
  PWSprefs::XMLify(charT('H'), hn);
  stringT un = si->GetEffectiveUser();
  PWSprefs::XMLify(charT('u'), un);

  stringT csHKCU_PREF = _T("Pwsafe_Settings\\");
  csHKCU_PREF += hn.c_str();
  csHKCU_PREF += _T("\\");
  csHKCU_PREF += un.c_str();
  csHKCU_PREF += _T("\\Preferences");

  bool rc = newXMLConfig.MigrateSettings(wsDefaultCfgFile, hn, un);
  if (rc) {
    // That worked, now remove us from the old one config file
    // in the Installation directory
    newXMLConfig.Unlock();

    // Since we now have new config file, remove host/user from old.
    if (!oldXMLConfig.XML_Load()) {
      rc = false;
      if (!oldXMLConfig.getReason().empty()) {
        CGeneralMsgBox gmb;
        gmb.SetMsg(oldXMLConfig.getReason().c_str());
        gmb.AddButton(IDS_CONTINUE, IDS_CONTINUE);
        gmb.AddButton(IDS_EXIT, IDS_EXIT, TRUE, TRUE);
        if (gmb.DoModal() == IDS_EXIT) {
          goto exit;
        }

        // Problem loading XML file but user says continue rather than Exit PWS!
        // But we will not remove them from the old file and we will
        // delete the new file - better luck next time!
        pws_os::DeleteAFile(wsDefaultCfgFile);
      }
    } // Load failed

    // Now remove this hostname/username from old configuration file in the
    // installation directory (as long as everything OK and it is not R-O)
    if (rc && !bExecCFRO) {
      rc = oldXMLConfig.RemoveHostnameUsername(hn, un, bNoMoreNodes);
      if (rc) {
        oldXMLConfig.XML_Store(csHKCU_PREF);

        // However, if no more host/user nodes in this file - delete the
        // configuration file from the installation directory!
        if (bNoMoreNodes) {
          pws_os::DeleteAFile(wsExecDirCfgFile);
        }

        bRetVal = true;
      } // RemoveHostnameUsername
    } // rc && !bExecCFRO
  } // MigrateSettings

  // If this all worked, now copy autoload_filters.xml if it exists and not
  // already in the new location.
  // This is ONLY done when we migrate the user's settings.
  if (bRetVal == true) {
    bool bALFRO(false);
    std::wstring wsOldAutoLoadFilters = wsExecDir + L"autoload_filters.xml";
    std::wstring wsNewAutoLoadFilters = wsUserCfgDir + L"autoload_filters.xml";
    if (pws_os::FileExists(wsOldAutoLoadFilters, bALFRO) &&
        !pws_os::FileExists(wsNewAutoLoadFilters)) {
      bool bCopyAutoloadFilters = pws_os::CopyAFile(wsOldAutoLoadFilters,
                                               wsNewAutoLoadFilters);

     // If we have copied it, there are no more nodes in the old configuration file
     // and it isn't read only - delete it from the installation directory
     if (bCopyAutoloadFilters && bNoMoreNodes && !bALFRO)
       pws_os::DeleteAFile(wsOldAutoLoadFilters);
    }
  }

  // Migration all done!

exit:
  // Clean up
  newXMLConfig.Unlock();
  oldXMLConfig.Unlock();

  // Set config file
  if (bRetVal)
    PWSprefs::SetConfigFile(wsDefaultCfgFile);
  return bRetVal;
}