Changeset 136 in main for branches/openPLM-0.2


Ignore:
Timestamp:
07/28/10 11:43:11 (10 years ago)
Author:
pcosquer
Message:

new branche 0.2

Location:
branches/openPLM-0.2
Files:
2 edited
1 copied

Legend:

Unmodified
Added
Removed
  • branches/openPLM-0.2/plugins/thunderbird/openplm/chrome.manifest

    r131 r136  
    33locale    openplm   en-US         chrome/locale/en-US/ 
    44overlay  chrome://messenger/content/messenger.xul  chrome://openplm/content/overlay.xul 
     5resource openplm modules/ 
    56 
    6  
  • branches/openPLM-0.2/plugins/thunderbird/openplm/chrome/content/overlay.js

    r135 r136  
    1 function Plugin(){ 
    2     this.username = ""; 
    3     this.password = ""; 
    4     this.SERVER =  "http://localhost:8000/";  
    5     this.xrequest = Components.classes["@mozilla.org/xmlextras/xmlhttprequest;1"] 
    6                             .createInstance(Components.interfaces.nsIXMLHttpRequest); 
    7  
    8     // methods 
    9     this.login = login; 
    10     this.send_post = send_post; 
    11     this.checkin = checkin; 
    12     this.add_file = add_file; 
    13     this.upload = upload; 
    14 } 
    15  
    16 function send_post(url, data) { 
    17     var params = ""; 
    18     for (var key in data){ 
    19         params += key + "=" + data[key] + "&"; 
    20     } 
    21     this.xrequest.open("POST", url, false); 
    22     this.xrequest.send(params); 
    23     var result = this.xrequest.responseText; 
    24     return JSON.parse(result); 
    25 } 
    26  
    27 function login(username, password){ 
    28     var url = this.SERVER + "api/login/"; 
    29     var res = this.send_post(url, {"username" : username, "password" : password}); 
    30     return res; 
    31 } 
    32  
    33 function add_file(doc, path){ 
    34     var id = doc["id"]; 
    35     var url = this.SERVER + "api/object/" + id + "/add_file/"; 
    36     this.upload(url, path); 
    37 } 
    38  
    39 function checkin(doc){ 
    40     paths = save_messages(); 
    41     var obj = this; 
    42     paths.forEach(function(path){ 
    43             obj.add_file(doc, path); 
    44             }); 
    45 } 
    46  
    47 function upload(url, filename) { 
    48     // open the local file 
    49     var file = Components.classes["@mozilla.org/file/local;1"] 
    50         .createInstance(Components.interfaces.nsILocalFile); 
    51     file.initWithPath( filename );       
    52    /* // Make a stream from a file. 
    53     var stream = Components.classes["@mozilla.org/network/file-input-stream;1"] 
    54         .createInstance(Components.interfaces.nsIFileInputStream); 
    55     stream.init(file, 0x04 | 0x08, 0600, 0x04); // file is an nsIFile instance    
    56  
    57     // Try to determine the MIME type of the file 
    58     var mimeType = "text/plain"; 
    59     try { 
    60         var mimeService = Components.classes["@mozilla.org/mime;1"] 
    61             .getService(Components.interfaces.nsIMIMEService); 
    62         mimeType = mimeService.getTypeFromFile(file); // file is an nsIFile instance 
    63     } 
    64     catch(e) {  } 
    65  
    66     */ 
    67  
    68     const BOUNDARY = "111222111"; //ce qui va nous servir de délimiteur 
    69     const MULTI    = "@mozilla.org/io/multiplex-input-stream;1"; 
    70     const FINPUT   = "@mozilla.org/network/file-input-stream;1"; 
    71     const STRINGIS = "@mozilla.org/io/string-input-stream;1"; 
    72     const BUFFERED = "@mozilla.org/network/buffered-input-stream;1"; 
    73     const nsIMultiplexInputStream = Components.interfaces.nsIMultiplexInputStream; 
    74     const nsIFileInputStream      = Components.interfaces.nsIFileInputStream; 
    75     const nsIStringInputStream    = Components.interfaces.nsIStringInputStream; 
    76     const nsIBufferedInputStream  = Components.interfaces.nsIBufferedInputStream; 
    77  
    78     var mis = Components.classes[MULTI].createInstance(nsIMultiplexInputStream); 
    79     var fin = Components.classes[FINPUT].createInstance(nsIFileInputStream); 
    80     fin.init(file, 0x01, 0444, null); //fic est un objet de type fichier 
    81     var buf = Components.classes[BUFFERED].createInstance(nsIBufferedInputStream); 
    82     buf.init(fin, 4096); 
    83     var hsis = Components.classes[STRINGIS].createInstance(nsIStringInputStream); 
    84     var sheader = new String(); 
    85     sheader += "--" + BOUNDARY + "\r\nContent-disposition: form-data;name=\"addfile\"\r\n\r\n1"; 
    86     sheader += "\r\n" + "--" + BOUNDARY + "\r\n"; 
    87     sheader += "Content-disposition: form-data;name=\"filename\";filename=\"" + file.leafName + "\"\r\n"; 
    88     sheader += "Content-Type: application/octet-stream\r\n"; 
    89     sheader += "Content-Length: " + file.fileSize+"\r\n\r\n"; 
    90      var converter = Components.classes["@mozilla.org/intl/scriptableunicodeconverter"] 
    91                 .createInstance(Components.interfaces.nsIScriptableUnicodeConverter); 
    92  
    93        converter.charset = "UTF-8"; 
    94     sheader = converter.ConvertFromUnicode(sheader); 
    95      
    96     hsis.setData(sheader, sheader.length); 
    97  
    98     var endsis = Components.classes[STRINGIS].createInstance(nsIStringInputStream); 
    99     var bs = new String("\r\n--" + BOUNDARY + "--\r\n"); 
    100     endsis.setData(bs, bs.length); 
    101  
    102     mis.appendStream(hsis); 
    103     mis.appendStream(buf); 
    104     mis.appendStream(endsis); 
    105     // Send 
    106     this.xrequest.open('POST', url, false); /* synchronous! */ 
    107     this.xrequest.setRequestHeader("Content-Length", mis.available()); 
    108     this.xrequest.setRequestHeader("Content-Type", "multipart/form-data; boundary=" + BOUNDARY); 
    109  
    110     this.xrequest.send(mis); 
    111     var result = this.xrequest.responseText; 
    112     alert(result); 
    113 } 
    114  
    115  
    116 var PLUGIN = new Plugin(); 
    117  
    118  
    119 // utilities 
    120 function GenerateFilenameFromMsgHdr(msgHdr) { 
    121  
    122     function MakeIS8601ODateString(date) { 
    123         function pad(n) {return n < 10 ? "0" + n : n;} 
    124         return date.getFullYear() + "-" + 
    125             pad(date.getMonth() + 1)  + "-" + 
    126             pad(date.getDate())  + " " + 
    127             pad(date.getHours())  + "" + 
    128             pad(date.getMinutes()) + ""; 
    129     } 
    130  
    131     let filename; 
    132     if (msgHdr.flags & Components.interfaces.nsMsgMessageFlags.HasRe) 
    133         filename = (msgHdr.mime2DecodedSubject) ? "Re: " + msgHdr.mime2DecodedSubject : "Re: "; 
    134     else 
    135         filename = msgHdr.mime2DecodedSubject; 
    136  
    137     filename += " - "; 
    138     filename += msgHdr.mime2DecodedAuthor  + " - "; 
    139     filename += MakeIS8601ODateString(new Date(msgHdr.date/1000)); 
    140  
    141     return filename; 
    142  
    143 } 
    144  
    145 function save_messages(){ 
    146     var nb = gFolderDisplay.selectedCount; 
    147     var paths = new Array(nb); 
    148     var messages = gFolderDisplay.selectedMessages; 
    149     if (nb == 0){ 
    150         return paths; 
    151     } 
    152     for (var i = 0; i < nb; i++){ 
    153         var content = ""; 
    154         var folder =  messages[0].folder; 
    155         var MessageURI = folder.getUriForMsg(messages[i]); 
    156         var MsgService = messenger.messageServiceFromURI(MessageURI); 
    157         var MsgStream = Components.classes["@mozilla.org/network/sync-stream-listener;1"].createInstance(); 
    158         var consumer = MsgStream.QueryInterface(Components.interfaces.nsIInputStream); 
    159         var ScriptInput = Components.classes["@mozilla.org/scriptableinputstream;1"].createInstance(); 
    160         var ScriptInputStream = ScriptInput.QueryInterface(Components.interfaces.nsIScriptableInputStream); 
    161         ScriptInputStream.init(consumer); 
    162         var file = Components.classes["@mozilla.org/file/directory_service;1"]. 
    163             getService(Components.interfaces.nsIProperties). 
    164             get("TmpD", Components.interfaces.nsIFile); 
    165         file.append(GenerateFilenameFromMsgHdr(messages[i])+".eml"); 
    166         file.createUnique(Components.interfaces.nsIFile.NORMAL_FILE_TYPE, 0600); 
    167         try { 
    168             MsgService.streamMessage(MessageURI, MsgStream, msgWindow, null, false, null); 
    169         } catch (ex) { 
    170             alert("error: "+ex) 
    171         } 
    172         ScriptInputStream.available(); 
    173         var foStream = Components.classes["@mozilla.org/network/file-output-stream;1"].   
    174             createInstance(Components.interfaces.nsIFileOutputStream);   
    175  
    176         // use 0x02 | 0x10 to open file for appending.  
    177         foStream.init(file, 0x02 | 0x08 | 0x20, 0600, 0); 
    178         var converter = Components.classes["@mozilla.org/intl/converter-output-stream;1"]. 
    179             createInstance(Components.interfaces.nsIConverterOutputStream); 
    180         converter.init(foStream, "UTF-8", 0, 0); 
    181         while (ScriptInputStream.available()) { 
    182             converter.writeString( ScriptInputStream .read(512)); 
    183         } 
    184         converter.close(); // this closes foStream 
    185         paths[i] = file.path; 
    186     } 
    187     return paths; 
    188  
    189 } 
     1Components.utils.import("resource://openplm/openplm.jsm");   
    1902 
    1913// interface 
     
    19810 
    19911onOpenPLMLogin: function(e) { 
    200  
     12                     
    20113                    var user = "user"; 
    20214                    var pw = "pass"; 
     
    21527                            return; 
    21628                        } 
    217                         var res = PLUGIN.login(username.value, password.value); 
     29                        var res = OPENPLM.login(username.value, password.value); 
    21830                        if (res["result"] != "ok"){ 
    21931                            user = username.value; 
     
    22638                }, 
    22739 
     40add_types: function(e){ 
     41               document.getElementById('document-list').appendItem("PLOPP"); 
     42           }, 
     43 
    22844onOpenPLMCheckIn: function(e) { 
    229                       PLUGIN.checkin({"id" : 30}); 
     45                      let someValue = 2; 
     46                      let returnValue = { accepted : false , result : "" }; 
     47 
     48                      window.openDialog( 
     49                                "chrome://openplm/content/checkin.xul", 
     50                                  "openplm-checkin-dialog", "chrome,centerscreen", 
     51                                    someValue, returnValue); // you can send as many extra parameters as you need. 
     52                      OPENPLM.checkin({"id" : 30}); 
    23053                  } 
    23154 
Note: See TracChangeset for help on using the changeset viewer.