root/trunk/modules/mediator.jsm

Revision 1085, 5.1 kB (checked in by MaierMan, 4 months ago)

fix for objToString

Line 
1 /* ***** BEGIN LICENSE BLOCK *****
2  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
3  *
4  * The contents of this file are subject to the Mozilla Public License Version
5  * 1.1 (the "License"); you may not use this file except in compliance with
6  * the License. You may obtain a copy of the License at
7  * http://www.mozilla.org/MPL/
8  *
9  * Software distributed under the License is distributed on an "AS IS" basis,
10  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11  * for the specific language governing rights and limitations under the
12  * License.
13  *
14  * The Original Code is DownThemAll Mediator module.
15  *
16  * The Initial Developer of the Original Code is Nils Maier
17  * Portions created by the Initial Developer are Copyright (C) 2008
18  * the Initial Developer. All Rights Reserved.
19  *
20  * Contributor(s):
21  *   Nils Maier <MaierMan@web.de>
22  *
23  * Alternatively, the contents of this file may be used under the terms of
24  * either the GNU General Public License Version 2 or later (the "GPL"), or
25  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
26  * in which case the provisions of the GPL or the LGPL are applicable instead
27  * of those above. If you wish to allow use of your version of this file only
28  * under the terms of either the GPL or the LGPL, and not to allow others to
29  * use your version of this file under the terms of the MPL, indicate your
30  * decision by deleting the provisions above and replace them with the notice
31  * and other provisions required by the GPL or the LGPL. If you do not delete
32  * the provisions above, a recipient may use your version of this file under
33  * the terms of any one of the MPL, the GPL or the LGPL.
34  *
35  * ***** END LICENSE BLOCK ***** */
36
37 const EXPORTED_SYMBOLS = [
38   'getMostRecent',
39   'getMostRecentByUrl',
40   'getAllByType',
41   'openExternal',
42   'openUrl',
43   'showNotice',
44 ];
45  
46 const Cc = Components.classes;
47 const Ci = Components.interfaces;
48 const Cr = Components.results;
49 const Cu = Components.utils;
50 const Exception = Components.Exception;
51
52 const mediator =  Cc['@mozilla.org/appshell/window-mediator;1'].getService(Ci.nsIWindowMediator);
53 const ioservice = Cc['@mozilla.org/network/io-service;1'].getService(Ci.nsIIOService);
54 const protoservice = Cc['@mozilla.org/uriloader/external-protocol-service;1'].getService(Ci.nsIExternalProtocolService);
55 const logger = Cc['@downthemall.net/debug-service;1'].getService(Ci.dtaIDebugService);
56 const windowwatcher = Cc["@mozilla.org/embedcomp/window-watcher;1"].getService(Ci.nsIWindowWatcher);
57
58
59 function objToString(obj) {
60   if (obj == null || obj == undefined) {
61     return obj;
62   }
63   if (
64     typeof obj == 'string'
65     || obj instanceof String
66   ) {
67     return obj.toString();
68   }
69   if (
70     obj instanceof Ci.nsIURL
71     || obj instanceof Ci.nsIURI
72   ) {
73     return obj.spec;
74   }
75   if (obj.url) {
76     return objToString(obj.url);
77   }
78   throw new Exception("Not a valid type");
79 }
80 function objToUri(obj) {
81   if (obj == null || obj == undefined) {
82     return null;
83   }
84   if (obj instanceof Ci.nsIURL || obj instanceof Ci.nsIURI) {
85     return obj;
86   }
87   if (typeof obj == 'string' || obj instanceof String) {
88     return ioservice.newURI(obj.toString(), null, null);
89   }
90   if (obj.url) {
91     return objToUri(obj.url);
92   }
93   throw new Exception("Not a valid type");
94 }
95
96 /**
97  * Gets the most recent window
98  * @param type Either a string or an array of string specifying the type of the window
99  */
100 function getMostRecent(type) {
101   if (type instanceof Array) {
102     for each (t in type) {
103       let rv = getMostRecent(t);
104       if (rv) {
105         return rv;
106       }
107     }
108   }
109   return mediator.getMostRecentWindow(type.toString());
110 }
111
112 /**
113  * Gets the most recent window by url instead of type
114  */
115 function getMostRecentByUrl(url) {
116   if (!url) {
117     return null;
118   }
119   url = objToString(url);
120
121   let enumerator = mediator.getEnumerator(null);
122   while (enumerator.hasMoreElements()) {
123     var win = enumerator.getNext();
124     if (win.location == url) {
125       return win;
126     }
127   }
128   return null
129 }
130
131 function getAllByType(type) {
132   let rv = [];
133   let enumerator = mediator.getEnumerator(type);
134   while (enumerator.hasMoreElements()) {
135     rv.push(enumerator.getNext());
136   }
137   return rv; 
138 }
139
140 function openExternal(link) {
141   logger.logString("Mediator: Using external handler for " + link);
142   protoservice.loadUrl(objToUri(link));
143 }
144 function openUrl(window, link, ref) {
145   logger.logString("Mediator: Request to open " + link);
146   try {
147     let win = getMostRecent('navigator:browser');
148     if (win) {
149       // browser
150       if ('delayedOpenTab' in win) {
151         win.delayedOpenTab(objToString(link), objToUri(ref));
152         return;
153       }
154       win.getBrowser().addTab(objToString(link), objToString(ref));
155       return;
156     }
157     win = getMostRecent('Songbird:Main');
158     if (win) {
159       // Songbird
160       let tb = win.document.getElementById('content');
161       if (tb) {
162         tb.loadOneTab(objToString(link), objToUri(ref), null, null, null);
163         return;
164       }
165     }
166   }
167   catch (ex) {
168     logger.log('Mediator: Failed to open tab', ex);
169   }
170   try {
171     window.open(objToString(link));
172   }
173   catch (ex) {
174     logger.log('Mediator: Failed to open window', ex);
175     openExternal(link);
176   }
177 }
178
179 function showNotice(window, params) {
180   windowwatcher.openWindow(
181     window,
182     'chrome://dta/content/about/notice.xul',
183     '_blank',
184     'chrome,centerscreen,all,dialog,modal',
185     params
186     );
187 }
Note: See TracBrowser for help on using the browser.