// vim: ts=4 sw=4 expandtab cindent foldmethod=marker
//
// BEGIN FLOCK GPL
// 
// Copyright Flock Inc. 2005-2006
// http://flock.com
// 
// This file may be used under the terms of of the
// GNU General Public License Version 2 or later (the "GPL"),
// http://www.gnu.org/licenses/gpl.html
// 
// Software distributed under the License is distributed on an "AS IS" basis,
// WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
// for the specific language governing rights and limitations under the
// License.
// 
// END FLOCK GPL
//


const flockIBlogWebService = Components.interfaces.flockIBlogWebService;
const nsISupports = Components.interfaces.nsISupports;
const FLOCK_BLOGGER_CID = Components.ID('{22c3051d-11e2-4a80-af67-5731a13addc3}');
const FLOCK_BLOGGER_CONTRACTID = '@flock.com/blog/service/blogger;1';
const FLOCK_BLOGGER_DESCRIPTION = 'Blogger';

// CONSTRUCTOR /*{{{*/
function bloggerWebService () {
    debug ('constructed bloggerWebService\n');
    this.mQueue = null;

    // set attributes
    this.configurationDialog = null;
    this.name = 'Blogger';
    this.description = "Google's blogging service"
    this.id = 'example';
    this.topbarPage = null;
}/*}}}*/

// the nsISupports implementation/*{{{*/
bloggerWebService.prototype.QueryInterface = function (iid) {
    if (iid.equals(flockIBlogWebService) || 
            iid.equals(nsISupports)) {
        return this;
    }

    throw Components.results.NS_ERROR_NO_INTERFACE;
}/*}}}*/

loadLibraryFromSpec("chrome://browser/content/flock/common/xmlrpc.js");
function loadLibraryFromSpec(aSpec) {
    var loader = Components.classes['@mozilla.org/moz/jssubscript-loader;1']
        .getService(Components.interfaces.mozIJSSubScriptLoader);

    loader.loadSubScript(aSpec);
}

// PRIVATE METHOD 
bloggerWebService.prototype.xmlrpc = function() {
    var listener = arguments[0];   
    var url = arguments[1];   
    var call = arguments[2];   

    var params = new Array();

    for(var i=3;i<arguments.length;++i)
    {
        params[params.length] = arguments[i];
    }
    var rpc = new JSXMLRPC();

    rpc.call(listener, url, call, params);
}

// IMPLEMENTATION
bloggerWebService.prototype.init = function (aName, aAppKey, aAPILink, aBlogID, aUsername, aPassword){
    // This is not Blogger specific.
    // TODO: Try to have a common implementation with other services
    // to avoid code duplication
    this.name = aName;
    this.appKey = aAppKey;
    this.editLink = "";
    this.apiLink = aAPILink;
    this.blogID = aBlogID;
    this.username = aUsername;
    this.password = aPassword;
}

bloggerWebService.prototype.setAuth = function (aUsername, aPassword){
    this.username = aUsername;
    this.password = aPassword;
}

bloggerWebService.prototype.newPost = function (aListener, aTitle, aDescription, aDateCreated, aPublish){
    return this.xmlrpc(aListener, this.apiLink, "blogger.newPost", this.appKey,
            this.blogid, this.username, this.password, aDescription, aPublish);
}

bloggerWebService.prototype.editPost = function (aListener, aPostid, aTitle, aDescription, aDateCreated, aPublish, aEditURI, aAtomID){ 
    return this.xmlrpc(aListener, this.apiLink, "blogger.editPost", this.appKey,
            aPostid, this.username, this.password, aDescription, aPublish);
}

bloggerWebService.prototype.deletePost = function (aListener, aPostid){
    return this.xmlrpc(aListener, this.apiLink, "blogger.deletePost", this.appKey,
            aPostid, this.username, this.password, true);
}

bloggerWebService.prototype.setPostCategories = function (aListener, aPostid, aCategories){
    aListener.onResult(null);
}

bloggerWebService.prototype.getPost = function (aListener, aPostid){
    throw new Error("blogger getPost() not implemented");
}

bloggerWebService.prototype.getUsersBlogs = function (aListener){
    return this.xmlrpc(aListener, this.apilink, "blogger.getUsersBlogs",
            this.appKey, this.username, this.password);
}

bloggerWebService.prototype.getRecentPosts = function (aListener, aNumber){
    return this.xmlrpc(aListener, this.apilink, "blogger.getRecentPosts", this.appKey,
            this.blogid, this.username, this.password, aNumber);
}

bloggerWebService.prototype.getCategoryList = function (aListener){
        aListener.onResult(null);
}


function loadLibrary(aFilename) { // {{{
    var file = Components.classes["@mozilla.org/file/directory_service;1"]
        .getService(Components.interfaces.nsIProperties)
        .get("ComsD", Components.interfaces.nsIFile);
    file.append(aFilename);

    var ios = Components.classes["@mozilla.org/network/io-service;1"]
        .getService(Components.interfaces.nsIIOService);
    var fileHandler = ios.getProtocolHandler("file")
        .QueryInterface(Components.interfaces.nsIFileProtocolHandler);
    var spec = fileHandler.getURLSpecFromFile(file);

    var scriptLoader = Components.classes["@mozilla.org/moz/jssubscript-loader;1"]
        .getService(Components.interfaces.mozIJSSubScriptLoader);

    scriptLoader.loadSubScript(spec);
}
// }}}


// Module implementation/*{{{*/
var BloggerModule = new Object();

BloggerModule.registerSelf =
function (compMgr, fileSpec, location, type)
{
    compMgr = compMgr.QueryInterface(Components.interfaces.nsIComponentRegistrar);

    compMgr.registerFactoryLocation(FLOCK_BLOGGER_CID, 
                                    "Flock Blogger JS Component",
                                    FLOCK_BLOGGER_CONTRACTID, 
                                    fileSpec, 
                                    location,
                                    type);
}

BloggerModule.getClassObject =
function (compMgr, cid, iid) {
    if (!cid.equals(FLOCK_BLOGGER_CID))
        throw Components.results.NS_ERROR_NO_INTERFACE;
    
    if (!iid.equals(Components.interfaces.nsIFactory))
        throw Components.results.NS_ERROR_NOT_IMPLEMENTED;
    
    return BloggerServiceFactory;
}

BloggerModule.canUnload =
function(compMgr)
{
    debug("Unloading shelf component.\n");
    //gPhotoService.dataSource.Flush();
    return true;
}/*}}}*/
    
/* factory object *//*{{{*/
var BloggerServiceFactory = new Object();

BloggerServiceFactory.createInstance =
function (outer, iid) {
    debug("CI: " + iid);
    if (outer != null)
        throw Components.results.NS_ERROR_NO_AGGREGATION;

    return (new bloggerWebService()).QueryInterface(iid);
}/*}}}*/

/* entrypoint *//*{{{*/
function NSGetModule(compMgr, fileSpec) {
    return BloggerModule;
}/*}}}*/

debug ('LOADED: flockBlogWebServiceBlogger.js\n');
