~ubuntu-branches/ubuntu/precise/kompozer/precise

« back to all changes in this revision

Viewing changes to mozilla/xpfe/components/killAll/nsKillAll.js

  • Committer: Bazaar Package Importer
  • Author(s): Anthony Yarusso
  • Date: 2007-08-27 01:11:03 UTC
  • Revision ID: james.westby@ubuntu.com-20070827011103-2jgf4s6532gqu2ka
Tags: upstream-0.7.10
ImportĀ upstreamĀ versionĀ 0.7.10

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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 Mozilla Kill All.
 
15
 *
 
16
 * The Initial Developer of the Original Code is
 
17
 * Netscape Communications Corp.
 
18
 * Portions created by the Initial Developer are Copyright (C) 2002
 
19
 * the Initial Developer. All Rights Reserved.
 
20
 *
 
21
 * Contributor(s):
 
22
 *   Bill Law  <law@netscape.com>
 
23
 *
 
24
 * Alternatively, the contents of this file may be used under the terms of
 
25
 * either the GNU General Public License Version 2 or later (the "GPL"), or
 
26
 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
 
27
 * in which case the provisions of the GPL or the LGPL are applicable instead
 
28
 * of those above. If you wish to allow use of your version of this file only
 
29
 * under the terms of either the GPL or the LGPL, and not to allow others to
 
30
 * use your version of this file under the terms of the MPL, indicate your
 
31
 * decision by deleting the provisions above and replace them with the notice
 
32
 * and other provisions required by the GPL or the LGPL. If you do not delete
 
33
 * the provisions above, a recipient may use your version of this file under
 
34
 * the terms of any one of the MPL, the GPL or the LGPL.
 
35
 *
 
36
 * ***** END LICENSE BLOCK ***** */
 
37
 
 
38
/* This file implements the nsICmdLineHandler interface.  See nsICmdLineHandler.idl
 
39
 * at http://lxr.mozilla.org/seamonkey/source/xpfe/appshell/public/nsICmdLineHandler.idl.
 
40
 *
 
41
 * This component handles the startup command line argument of the form:
 
42
 *   -killAll
 
43
 * by closing all windows and shutting down turbo mode.
 
44
 *
 
45
 * The module is registered under the contractid
 
46
 *   "@mozilla.org/commandlinehandler/general-startup;1?type=killAll"
 
47
 *
 
48
 * The implementation consists of a JavaScript "class" named nsKillAll,
 
49
 * comprised of:
 
50
 *   - a JS constructor function
 
51
 *   - a prototype providing all the interface methods and implementation stuff
 
52
 *
 
53
 * In addition, this file implements an nsIModule object that registers the
 
54
 * nsKillAll component.
 
55
 */
 
56
 
 
57
/* ctor
 
58
 */
 
59
function nsKillAll() {
 
60
}
 
61
 
 
62
nsKillAll.prototype = {
 
63
 
 
64
    // nsICmdLineHandler interface
 
65
    get commandLineArgument() { throw Components.results.NS_ERROR_NOT_IMPLEMENTED; },
 
66
    get prefNameForStartup()  { throw Components.results.NS_ERROR_NOT_IMPLEMENTED; },
 
67
 
 
68
    get chromeUrlForTask()    { 
 
69
 
 
70
      // turn off server mode
 
71
 
 
72
      var wasMozillaAlreadyRunning = false;
 
73
      var appShellService = Components.classes["@mozilla.org/appshell/appShellService;1"]
 
74
                           .getService(Components.interfaces.nsIAppShellService);
 
75
      var nativeAppSupport = {isServerMode: false};
 
76
      try {
 
77
        nativeAppSupport = appShellService.nativeAppSupport;
 
78
      } catch ( ex ) {
 
79
      }
 
80
 
 
81
      var originalServerMode = false;
 
82
      if (nativeAppSupport.isServerMode) {
 
83
        originalServerMode = true;
 
84
        wasMozillaAlreadyRunning = true;
 
85
        nativeAppSupport.isServerMode = false;
 
86
      }
 
87
 
 
88
      // stop all applications
 
89
 
 
90
      var gObserverService = Components.classes["@mozilla.org/observer-service;1"]
 
91
                             .getService(Components.interfaces.nsIObserverService);
 
92
      if (gObserverService) {
 
93
        try {
 
94
          gObserverService.notifyObservers(null, "quit-application", null);
 
95
        } catch (ex) {
 
96
          // dump("no observer found \n");
 
97
        }
 
98
      }
 
99
 
 
100
      // close down all windows
 
101
 
 
102
      var windowManager =
 
103
        Components.classes['@mozilla.org/appshell/window-mediator;1']
 
104
        .getService(Components.interfaces.nsIWindowMediator);
 
105
      var enumerator = windowManager.getEnumerator(null);
 
106
      while(enumerator.hasMoreElements()) {
 
107
        wasMozillaAlreadyRunning = true;
 
108
        var domWindow = enumerator.getNext();
 
109
        if (("tryToClose" in domWindow) && !domWindow.tryToClose()) {
 
110
          // user pressed cancel in response to dialog for closing an unsaved window
 
111
          nativeAppSupport.isServerMode = originalServerMode
 
112
          // Return NS_ERROR_ABORT to inform caller of the "cancel."
 
113
          throw Components.results.NS_ERROR_ABORT;
 
114
        }
 
115
        domWindow.close();
 
116
      }
 
117
 
 
118
      // exit without opening a new window
 
119
 
 
120
      if (wasMozillaAlreadyRunning) {
 
121
        // Need to exit appshell in this case.
 
122
        appShellService.quit(Components.interfaces.nsIAppShellService.eAttemptQuit);
 
123
      }
 
124
 
 
125
      // We throw NS_ERROR_NOT_AVAILABLE which will be interpreted by the caller
 
126
      // to mean that the command line handler is indicating that this should be a
 
127
      // silent command and not cause any windows to open.
 
128
      throw Components.results.NS_ERROR_NOT_AVAILABLE;
 
129
    },
 
130
 
 
131
    get helpText()            { throw Components.results.NS_ERROR_NOT_IMPLEMENTED; },
 
132
    get handlesArgs()         { throw Components.results.NS_ERROR_NOT_IMPLEMENTED; }, 
 
133
    get defaultArgs()         { throw Components.results.NS_ERROR_NOT_IMPLEMENTED; }, 
 
134
    get openWindowWithArgs()  { throw Components.results.NS_ERROR_NOT_IMPLEMENTED; }, 
 
135
 
 
136
    // nsISupports interface
 
137
 
 
138
    // This "class" supports nsICmdLineHandler and nsISupports.
 
139
    QueryInterface: function (iid) {
 
140
        if (!iid.equals(Components.interfaces.nsICmdLineHandler) &&
 
141
            !iid.equals(Components.interfaces.nsISupports)) {
 
142
            throw Components.results.NS_ERROR_NO_INTERFACE;
 
143
        }
 
144
        return this;
 
145
    },
 
146
 
 
147
    // This Component's module implementation.  All the code below is used to get this
 
148
    // component registered and accessible via XPCOM.
 
149
    module: {
 
150
        // registerSelf: Register this component.
 
151
        registerSelf: function (compMgr, fileSpec, location, type) {
 
152
            var compReg = compMgr.QueryInterface( Components.interfaces.nsIComponentRegistrar );
 
153
            compReg.registerFactoryLocation( this.cid,
 
154
                                             "Kill All Component",
 
155
                                             this.contractId,
 
156
                                             fileSpec,
 
157
                                             location,
 
158
                                             type );
 
159
        },
 
160
    
 
161
        // getClassObject: Return this component's factory object.
 
162
        getClassObject: function (compMgr, cid, iid) {
 
163
            if (!cid.equals(this.cid))
 
164
                throw Components.results.NS_ERROR_NO_INTERFACE;
 
165
    
 
166
            if (!iid.equals(Components.interfaces.nsIFactory))
 
167
                throw Components.results.NS_ERROR_NOT_IMPLEMENTED;
 
168
    
 
169
            return this.factory;
 
170
        },
 
171
    
 
172
        /* CID for this class */
 
173
        cid: Components.ID("{F1F25940-4C8F-11d6-A651-0010A401EB10}"),
 
174
    
 
175
        /* Contract ID for this class */
 
176
        contractId: "@mozilla.org/commandlinehandler/general-startup;1?type=killAll",
 
177
    
 
178
        /* factory object */
 
179
        factory: {
 
180
            // createInstance: Return a new nsKillAll object.
 
181
            createInstance: function (outer, iid) {
 
182
                if (outer != null)
 
183
                    throw Components.results.NS_ERROR_NO_AGGREGATION;
 
184
    
 
185
                return (new nsKillAll()).QueryInterface(iid);
 
186
            }
 
187
        },
 
188
    
 
189
        // canUnload: n/a (returns true)
 
190
        canUnload: function(compMgr) {
 
191
            return true;
 
192
        }
 
193
    }
 
194
}
 
195
 
 
196
// NSGetModule: Return the nsIModule object.
 
197
function NSGetModule(compMgr, fileSpec) {
 
198
    return nsKillAll.prototype.module;
 
199
}