/*
Function type: Custom
Gesture name: Import Gesture Set
Custom JavaScript code:
*/

var importWarning = confirm("You want to import a gesture set.\n" +
                            "Make sure that you have a valid file to import!\n\n" +
                            "If the chosen file is not valid, the default gesture set will be restored!");

if (importWarning)
{
  var title = "MozGest - Gesture Import";
  var cc = Components.classes;
  var ci = Components.interfaces;

  if (mgStorage.userDS == null)
    mgStorage.getUserRDF();

  var profile = cc['@mozilla.org/file/directory_service;1'].getService(ci.nsIProperties);

  var filePath = profile.get('ProfD', ci.nsILocalFile);

  var fp = cc["@mozilla.org/filepicker;1"].createInstance(ci.nsIFilePicker);
  fp.init(window, title, ci.nsIFilePicker.modeOpen);

  var retVal = fp.show();

  if (retVal == ci.nsIFilePicker.returnOK)
  {
    try
    {
      var mgPath = profile.get('ProfD', ci.nsILocalFile);
      mgPath.appendRelativePath("mousegestures.rdf");
      mgPath.remove(false);
      fp.file.copyTo(filePath, "mousegestures.rdf");
      try
      {
        mgStorage.userDS.QueryInterface(ci.nsIRDFRemoteDataSource).Refresh(true);
      }
      catch (e)
      {
        alert("The backup is corrupted. Default gestures restored.")
      }

      var restart = confirm("Gestures restored.\n You have to restart "
                            + mgAppInfo.name + "!\n\n"
                            + "Do you want to restart now?");
      if (restart)
      {
        var appStartup = Components.interfaces.nsIAppStartup;
        cc["@mozilla.org/toolkit/app-startup;1"].getService(appStartup)
                        .quit(appStartup.eRestart | appStartup.eAttemptQuit);
      }
    }
    catch (e) {}
  }
}

