1 /* See license.txt for terms of usage */
  2 
  3 define([
  4     "firebug/lib/trace",
  5     "firebug/lib/array",
  6 ],
  7 function(FBTrace, Arr) {
  8 
  9 // ********************************************************************************************* //
 10 // Constants
 11 
 12 var Ci = Components.interfaces;
 13 var Cc = Components.classes;
 14 var Cu = Components.utils;
 15 
 16 var ioService = Cc["@mozilla.org/network/io-service;1"].getService(Ci.nsIIOService);
 17 
 18 var System = {};
 19 
 20 // ********************************************************************************************* //
 21 
 22 System.getPlatformName = function()
 23 {
 24     return Cc["@mozilla.org/xre/app-info;1"].getService(Ci.nsIXULRuntime).OS;
 25 };
 26 
 27 System.beep = function()
 28 {
 29     var sounder = Cc["@mozilla.org/sound;1"].getService(Ci.nsISound);
 30     sounder.beep();
 31 };
 32 
 33 // ********************************************************************************************* //
 34 // Programs
 35 
 36 System.launchProgram = function(exePath, args)
 37 {
 38     try
 39     {
 40         var file = Cc["@mozilla.org/file/local;1"].createInstance(Ci.nsIFile);
 41         file.initWithPath(exePath);
 42         if (System.getPlatformName() == "Darwin" && file.isDirectory())
 43         {
 44             args = Arr.extendArray(["-a", exePath], args);
 45             file.initWithPath("/usr/bin/open");
 46         }
 47 
 48         if (!file.exists())
 49             return false;
 50 
 51         var process = Cc["@mozilla.org/process/util;1"].createInstance(Ci.nsIProcess);
 52         process.init(file);
 53         process.run(false, args, args.length, {});
 54         return true;
 55     }
 56     catch(exc)
 57     {
 58         this.ERROR(exc);
 59     }
 60     return false;
 61 };
 62 
 63 System.getIconURLForFile = function(path)
 64 {
 65     var fileHandler = ioService.getProtocolHandler("file")
 66         .QueryInterface(Ci.nsIFileProtocolHandler);
 67 
 68     try
 69     {
 70         var file = Cc["@mozilla.org/file/local;1"].createInstance(Ci.nsIFile);
 71         file.initWithPath(path);
 72         if ((System.getPlatformName() == "Darwin") && !file.isDirectory() &&
 73             (path.indexOf(".app/") != -1))
 74         {
 75             path = path.substr(0,path.lastIndexOf(".app/")+4);
 76             file.initWithPath(path);
 77         }
 78 
 79         return "moz-icon://" + fileHandler.getURLSpecFromFile(file) + "?size=16";
 80     }
 81     catch (exc)
 82     {
 83         if (FBTrace.DBG_ERRORS)
 84             FBTrace.sysout("getIconURLForFile ERROR "+exc+" for "+path, exc);
 85     }
 86 
 87     return null;
 88 }
 89 
 90 System.copyToClipboard = function(string)
 91 {
 92     var clipboard = Cc["@mozilla.org/widget/clipboardhelper;1"].getService(Ci.nsIClipboardHelper);
 93     clipboard.copyString(string);
 94 
 95     if (FBTrace.DBG_ERRORS && !string)
 96         FBTrace.sysout("system.copyToClipboard; " + string, string);
 97 };
 98 
 99 System.getStringDataFromClipboard = function()
100 {
101     // https://developer.mozilla.org/en-US/docs/Using_the_Clipboard#Pasting_Clipboard_Contents
102     var clip = Cc["@mozilla.org/widget/clipboard;1"].getService(Ci.nsIClipboard);
103     if (!clip)
104         return false;
105 
106     var trans = Cc["@mozilla.org/widget/transferable;1"].createInstance(Ci.nsITransferable);
107     if (!trans)
108         return false;
109 
110     if ("init" in trans)
111         trans.init(null);
112 
113     trans.addDataFlavor("text/unicode");
114 
115     clip.getData(trans, clip.kGlobalClipboard);
116 
117     var str = {};
118     var strLength = {};
119 
120     try
121     {
122         trans.getTransferData("text/unicode", str, strLength);
123 
124         if (str)
125         {
126             str = str.value.QueryInterface(Ci.nsISupportsString);
127             return str.data.substring(0, strLength.value / 2);
128         }
129     }
130     catch (ex)
131     {
132     }
133 
134     return false;
135 }
136 
137 // ********************************************************************************************* //
138 // Firebug Version Comparator
139 
140 // xxxFlorent: IMO, wrong place...
141 /**
142  * Compare expected Firebug version with the current Firebug installed.
143  * @param {Object} expectedVersion Expected version of Firebug.
144  * @returns
145  * -1 the current version is smaller
146  *  0 the current version is the same
147  *  1 the current version is bigger
148  *
149  * @example:
150  * if (compareFirebugVersion("1.9") >= 0)
151  * {
152  *     // The current version is Firebug 1.9+
153  * }
154  */
155 System.checkFirebugVersion = function(expectedVersion)
156 {
157     if (!expectedVersion)
158         return 1;
159 
160     var version = Firebug.getVersion();
161 
162     // Use Firefox comparator service.
163     var versionChecker = Cc["@mozilla.org/xpcom/version-comparator;1"].
164         getService(Ci.nsIVersionComparator);
165     return versionChecker.compare(version, expectedVersion);
166 }
167 
168 // ********************************************************************************************* //
169 
170 return System;
171 
172 // ********************************************************************************************* //
173 });
174