1 /* See license.txt for terms of usage */
  2 
  3 // ************************************************************************************************
  4 // Constants
  5 
  6 const prefs = fbXPCOMUtils.CCSV("@mozilla.org/preferences-service;1", "nsIPrefBranch");
  7 const names = ["label", "executable", "cmdline", "image"];
  8 
  9 // ************************************************************************************************
 10 // Globals
 11 
 12 var gEditorManager =
 13 {
 14     _tree : null,
 15     _data : [],
 16     _removeButton : null,
 17     _changeButton : null,
 18 
 19     init: function()
 20     {
 21         var args = window.arguments[0];
 22         this._FBL = args.FBL;
 23         this._prefName = args.prefName;
 24 
 25         (this._removeButton = document.getElementById("removeEditor")).disabled = true;
 26         (this._changeButton = document.getElementById("changeEditor")).disabled = true;
 27 
 28         this._tree = document.getElementById("editorsList");
 29 
 30         this._treeView =
 31         {
 32             data: this._data,
 33             selection: null,
 34 
 35             get rowCount() { return this.data.length; },
 36             getCellText: function(row, column)
 37             {
 38                 switch(column.id)
 39                 {
 40                 case "editorName":
 41                     return " "+this.data[row].label;
 42                 case "editorExecutable":
 43                     return this.data[row].executable;
 44                 case "editorParams":
 45                     return this.data[row].cmdline;
 46                 }
 47                 return "";
 48             },
 49             setTree: function(treebox){ this.treebox = treebox; },
 50             isContainer: function(row) { return false; },
 51             isContainerOpen: function(row) { return false; },
 52             isContainerEmpty: function(row) { return false; },
 53             isSeparator: function(row) { return false; },
 54             isSorted: function() { return false; },
 55             getLevel: function(row) { return 0; },
 56             getImageSrc: function(row,column) { return column.id=="editorName" ? this.data[row].image : null; },
 57             getRowProperties: function(row,props) {},
 58             getCellProperties: function(row,column,props) {},
 59             getColumnProperties: function(colid,column,props) {}
 60         };
 61 
 62         this._load();
 63         this._tree.view = this._treeView;
 64     },
 65 
 66     uninit: function()
 67     {
 68     },
 69 
 70     onSelectionChanged: function()
 71     {
 72         var selection = this._tree.view.selection;
 73         this._removeButton.disabled = (selection.count != 1);
 74         this._changeButton.disabled = (selection.count != 1);
 75     },
 76 
 77     addEditorHandler: function()
 78     {
 79         var item = { label: "", executable: null, cmdline: "" };
 80         var result = {};
 81         var args = {
 82             item: item,
 83             FBL: this._FBL
 84         };
 85         openDialog("chrome://firebug/content/changeeditor.xul",  "_blank", "modal,centerscreen", args, result);
 86         if (result.saveChanges)
 87         {
 88             item.id = item.label.replace(/\W/g, "_");
 89             this._saveItem(item);
 90 
 91             this._loadItem(item);
 92             this._data.push(item);
 93             this._tree.view = this._treeView;
 94 
 95             var editors = [];
 96             try {
 97                 editors = prefs.getCharPref(this._prefName).split(",");
 98                 for( var i = 0; i < editors.length; ++i )
 99                 {
100                     if ( editors[i].replace(/^\s+|\s+$/,"") == "" )
101                         editors.splice(i, 1);
102                 }
103             }
104             catch(exc)
105             {
106                 this._FBL.ERROR(exc);
107             }
108             editors.push(item.id);
109             prefs.setCharPref(this._prefName, editors.join(","));
110         }
111     },
112 
113     removeEditorHandler: function()
114     {
115         var selection = this._tree.view.selection;
116         if (selection.count < 1)
117             return;
118         var item = this._data[selection.currentIndex];
119         this._data.splice(selection.currentIndex, 1);
120         this._tree.view = this._treeView;
121 
122         try {
123             var editors = prefs.getCharPref(this._prefName).split(",");
124             this._FBL.remove(editors, item.id);
125             prefs.setCharPref(this._prefName, editors.join(","));
126             prefs.deleteBranch(this._prefName+"."+item.id);
127         }
128         catch(exc)
129         {
130             this._FBL.ERROR(exc);
131         }
132     },
133 
134     changeEditorHandler: function()
135     {
136         var selection = this._tree.view.selection;
137         if (selection.count != 1)
138             return;
139         var item = this._data[selection.currentIndex];
140         var args = {
141             item: item,
142             FBL: this._FBL
143         };
144         var result = {};
145         openDialog("chrome://firebug/content/changeeditor.xul",  "_blank", "modal,centerscreen", args, result);
146         if (result.saveChanges)
147         {
148             this._saveItem(item);
149         }
150         this._loadItem(item);
151         this._tree.view = this._treeView;
152     },
153 
154     _loadItem: function(item)
155     {
156         const prefName = this._prefName;
157         for( var i = 0; i < names.length; ++i )
158         {
159             try {
160                 item[names[i]] = prefs.getCharPref(prefName+"."+item.id+"."+names[i]);
161             }
162             catch(exc)
163             {}
164         }
165         if (!item.image)
166             item.image = this._FBL.getIconURLForFile(item.executable);
167     },
168 
169     _saveItem: function(item)
170     {
171         if ( item.image && item.image == this._FBL.getIconURLForFile(item.executable) )
172             item.image = null;
173 
174         const prefName = this._prefName;
175         for( var i = 0; i < names.length; ++i )
176         {
177             try {
178                 var value = item[names[i]];
179                 if ( value )
180                     prefs.setCharPref(prefName+"."+item.id+"."+names[i], value);
181                 else
182                     prefs.clearUserPref(prefName+"."+item.id+"."+names[i]);
183             }
184             catch(exc)
185             {}
186         }
187     },
188 
189     _load: function()
190     {
191         try {
192             var list = prefs.getCharPref(this._prefName).split(",");
193             for (var i = 0; i < list.length; ++i)
194             {
195                 var editorId = list[i].replace(/\s/g, "_");
196                 if ( !editorId )
197                     continue;
198                 var item = { id: editorId };
199                 this._data.push(item);
200                 this._loadItem(item);
201             }
202         }
203         catch(exc)
204         {
205             this._FBL.ERROR(exc);
206         }
207     }
208 
209 };
210 
211 // ************************************************************************************************
212