home *** CD-ROM | disk | FTP | other *** search
/ PC World 2008 May / PCWorld_2008-05_cd.bin / komunikace / sameplace / sameplace-suite-release.xpi / sameplace-0.9.1.xpi / components / ProtocolHandler.js < prev    next >
Encoding:
Text File  |  2008-02-11  |  7.0 KB  |  265 lines

  1. /* ---------------------------------------------------------------------- */
  2. /*                        Protocol specific code                          */
  3.  
  4. const kSCHEME = 'xmpp';
  5. const kPROTOCOL_NAME = 'XMPP Protocol';
  6. const kPROTOCOL_CID = Components.ID('{101008f2-4454-4bdb-9f3a-ae3b02eeb20d}');
  7. const kCONTENT_NAME = 'XMPP Content Handler';
  8. const kCONTENT_CID = Components.ID('{2d12cfe5-8cdd-49bb-b766-e551343c1265}');
  9.  
  10.  
  11. this.__defineGetter__(
  12.     'XMPP', function() {
  13.         if(!arguments.callee.XMPP)
  14.             loader.loadSubScript('chrome://xmpp4moz/content/xmpp.js', arguments.callee);
  15.  
  16.         return arguments.callee.XMPP;
  17.     });
  18.  
  19. function xpcomize(thing) {
  20.     if(typeof(thing) == 'string') {
  21.         var xpcomString = Cc["@mozilla.org/supports-string;1"]
  22.             .createInstance(Ci.nsISupportsString);
  23.         xpcomString.data = thing;
  24.         return xpcomString;
  25.     } else if(thing instanceof Ci.nsISupports) {
  26.         return thing;
  27.     } else {
  28.         throw new Error('Neither an XPCOM object nor a string. (' + thing + ')');
  29.     }
  30. }
  31.  
  32. function onNewChannel(URI) {
  33.     var m = URI.spec.match(/^xmpp:([^\?$]+)(\?\w+)?(;.+)?$/);
  34.     var jid = encodeURI(m[1]);
  35.  
  36.     switch(m[2]) {
  37.     case '?roster':
  38.     case '?remove':
  39.     case '?subscribe':
  40.     case '?unsubscribe':
  41.  
  42.     case '?join':
  43.     case '?message':
  44.     default:
  45.         var array = Cc['@mozilla.org/supports-array;1'].createInstance(Ci.nsISupportsArray);
  46.         array.AppendElement(null);
  47.         array.AppendElement(xpcomize(jid));
  48.  
  49.         ww.openWindow(null, 'chrome://sameplace/content/dialogs/' +
  50.                       (m[2] == '?join' ? 'join_room.xul' : 'open_conversation.xul'),
  51.                       null, '', array);
  52.     }    
  53.  
  54.     return new Channel(URI);
  55. }
  56.  
  57.  
  58.  
  59. /* ---------------------------------------------------------------------- */
  60. /*                            Template code                               */ 
  61.  
  62. const kPROTOCOL_CONTRACTID = '@mozilla.org/network/protocol;1?name=' + kSCHEME;
  63. const kSIMPLEURI_CONTRACTID = '@mozilla.org/network/simple-uri;1';
  64. const kIOSERVICE_CONTRACTID = '@mozilla.org/network/io-service;1';
  65. const kCONTENT_CONTRACTID = '@mozilla.org/uriloader/content-handler;1?type=x-application-' + kSCHEME;
  66.  
  67. const Cc = Components.classes;
  68. const Ci = Components.interfaces;
  69. const Cr = Components.results;
  70. const loader = Cc['@mozilla.org/moz/jssubscript-loader;1']
  71.     .getService(Ci.mozIJSSubScriptLoader);
  72. const ww = Cc['@mozilla.org/embedcomp/window-watcher;1']
  73.     .getService(Ci.nsIWindowWatcher);
  74.  
  75.  
  76. // PROTOCOL HANDLER
  77. // ----------------------------------------------------------------------
  78.  
  79. function ProtocolHandler() {}
  80.  
  81. ProtocolHandler.prototype = {
  82.     QueryInterface: function(iid) {
  83.         if(!iid.equals(Ci.nsIProtocolHandler) &&
  84.            !iid.equals(Ci.nsISupports))
  85.             throw Cr.NS_ERROR_NO_INTERFACE;
  86.  
  87.         return this;
  88.     },
  89.  
  90.     scheme: kSCHEME,
  91.  
  92.     defaultPort: -1,
  93.  
  94.     protocolFlags: Ci.nsIProtocolHandler.URI_NORELATIVE | Ci.nsIProtocolHandler.URI_NOAUTH,
  95.   
  96.     allowPort: function(port, scheme) {
  97.         return false;
  98.     },
  99.  
  100.     newURI: function(spec, charset, baseURI) {
  101.         var uri = Components.classes[kSIMPLEURI_CONTRACTID].createInstance(Ci.nsIURI);
  102.         uri.spec = spec;
  103.         return uri;
  104.     },
  105.  
  106.     newChannel: function(URI) {
  107.         return onNewChannel(URI);
  108.     }
  109. };
  110.  
  111.  
  112. // CONTENT HANDLER
  113. //----------------------------------------------------------------------
  114.  
  115. function ContentHandler() {}
  116.  
  117. ContentHandler.prototype = {
  118.     QueryInterface: function(iid) {
  119.         if(!iid.equals(Ci.nsIContentHandler))
  120.             throw Cr.NS_ERROR_NO_INTERFACE;
  121.         
  122.         return this;
  123.     },
  124.  
  125.     handleContent: function (contentType, windowTarget, request, aRemovedArg) {
  126.  
  127.     }
  128. };
  129.  
  130.  
  131. // CHANNEL
  132. // ----------------------------------------------------------------------
  133.  
  134. function Channel(URI) {
  135.    this.URI = URI;
  136.    this.originalURI = URI;
  137. }
  138.  
  139. Channel.prototype = {
  140.     QueryInterface: function(iid) {
  141.         if(!iid.equals(Ci.nsIChannel) &&
  142.            !iid.equals(Ci.nsIRequest) &&
  143.            !iid.equals(Ci.nsISupports))
  144.             throw Cr.NS_ERROR_NO_INTERFACE;
  145.         
  146.         return this;
  147.     },
  148.  
  149.     /* nsIChannel */
  150.     loadAttributes: null,
  151.     contentType: 'x-application-' + kSCHEME,
  152.     contentLength: 0,
  153.     owner: null,
  154.     loadGroup: null,
  155.     notificationCallbacks: null,
  156.     securityInfo: null,
  157.  
  158.     open: function() {
  159.         throw Cr.NS_ERROR_NOT_IMPLEMENTED; 
  160.     },
  161.  
  162.     asyncOpen: function(observer, ctxt) {
  163.         //observer.onStartRequest(this, ctxt);
  164.     },
  165.  
  166.     asyncRead: function(listener, ctxt) {
  167.         return listener.onStartRequest(this, ctxt);
  168.     },
  169.  
  170.     /* nsIRequest */
  171.  
  172.     status: Cr.NS_OK,
  173.  
  174.     isPending: function() { 
  175.         return true; 
  176.     },
  177.  
  178.     cancel: function(status) {
  179.         this.status = status;
  180.     },
  181.  
  182.     suspend: function() {
  183.         throw Cr.NS_ERROR_NOT_IMPLEMENTED;
  184.     },
  185.  
  186.     resume: function() {
  187.         throw Cr.NS_ERROR_NOT_IMPLEMENTED;
  188.     }
  189. };
  190.  
  191.  
  192. // FACTORIES
  193. // ----------------------------------------------------------------------
  194.  
  195. var ProtocolHandlerFactory = {
  196.     createInstance: function(outer, iid) {
  197.         if(outer != null)
  198.             throw Cr.NS_ERROR_NO_AGGREGATION;
  199.         
  200.         if(!iid.equals(Ci.nsIProtocolHandler) &&
  201.            !iid.equals(Ci.nsISupports))
  202.             throw Cr.NS_ERROR_NO_INTERFACE;
  203.  
  204.         return new ProtocolHandler();
  205.     }
  206. };
  207.  
  208. var ContentHandlerFactory = {
  209.     createInstance: function(outer, iid) {
  210.         if (outer != null) {
  211.             throw Cr.NS_ERROR_NO_AGGREGATION;
  212.         }
  213.         
  214.         if (!iid.equals(Ci.nsIContentHandler) &&
  215.             !iid.equals(Ci.nsISupports)) {
  216.             throw Cr.NS_ERROR_INVALID_ARG;
  217.         }
  218.         return new ContentHandler();
  219.     }
  220. };
  221.  
  222.  
  223. // MODULE
  224. // ----------------------------------------------------------------------
  225.  
  226. var Module = {
  227.     registerSelf: function(compMgr, fileSpec, location, type) {
  228.         compMgr = compMgr.QueryInterface(Components.interfaces.nsIComponentRegistrar);
  229.         compMgr.registerFactoryLocation(kPROTOCOL_CID,
  230.                                         kPROTOCOL_NAME,
  231.                                         kPROTOCOL_CONTRACTID,
  232.                                         fileSpec, 
  233.                                         location, 
  234.                                         type);
  235.         compMgr.registerFactoryLocation(kCONTENT_CID,
  236.                                         kCONTENT_NAME,
  237.                                         kCONTENT_CONTRACTID,
  238.                                         fileSpec, 
  239.                                         location, 
  240.                                         type);
  241.     },
  242.  
  243.     getClassObject: function(compMgr, cid, iid) {
  244.         if(!cid.equals(kPROTOCOL_CID))
  245.             throw Cr.NS_ERROR_NO_INTERFACE;
  246.  
  247.         if(!iid.equals(Ci.nsIFactory))
  248.             throw Cr.NS_ERROR_NOT_IMPLEMENTED;
  249.  
  250.         if(cid.equals(kCONTENT_CID))
  251.             return ContentHandlerFactory;
  252.         else if(cid.equals(kPROTOCOL_CID))
  253.             return ProtocolHandlerFactory;
  254.     },
  255.  
  256.     canUnload: function(compMgr) {
  257.         return true;
  258.     }
  259. };
  260.  
  261. function NSGetModule(compMgr, fileSpec) {
  262.     return Module;
  263. }
  264.  
  265.