Back to index

lightning-sunbird  0.9+nobinonly
mozilla-xremote-client.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
00002 /* vim:expandtab:shiftwidth=4:tabstop=4:
00003  */
00004 /* ***** BEGIN LICENSE BLOCK *****
00005  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00006  *
00007  * The contents of this file are subject to the Mozilla Public License Version
00008  * 1.1 (the "License"); you may not use this file except in compliance with
00009  * the License. You may obtain a copy of the License at
00010  * http://www.mozilla.org/MPL/
00011  *
00012  * Software distributed under the License is distributed on an "AS IS" basis,
00013  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00014  * for the specific language governing rights and limitations under the
00015  * License.
00016  *
00017  * The Original Code is mozilla.org code.
00018  *
00019  * The Initial Developer of the Original Code is Christopher Blizzard
00020  * <blizzard@mozilla.org>.  Portions created by the Initial Developer
00021  * are Copyright (C) 2001 the Initial Developer. All Rights Reserved.
00022  *
00023  * Contributor(s):
00024  *
00025  * Alternatively, the contents of this file may be used under the terms of
00026  * either the GNU General Public License Version 2 or later (the "GPL"), or
00027  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00028  * in which case the provisions of the GPL or the LGPL are applicable instead
00029  * of those above. If you wish to allow use of your version of this file only
00030  * under the terms of either the GPL or the LGPL, and not to allow others to
00031  * use your version of this file under the terms of the MPL, indicate your
00032  * decision by deleting the provisions above and replace them with the notice
00033  * and other provisions required by the GPL or the LGPL. If you do not delete
00034  * the provisions above, a recipient may use your version of this file under
00035  * the terms of any one of the MPL, the GPL or the LGPL.
00036  *
00037  * ***** END LICENSE BLOCK ***** */
00038 
00039 #include <stdio.h>
00040 #include <stdlib.h>
00041 #include <string.h>
00042 #include <plgetopt.h>
00043 #ifdef MOZ_WIDGET_PHOTON
00044 #include "PhRemoteClient.h"
00045 #else
00046 #include "XRemoteClient.h"
00047 #endif
00048 
00049 static void print_usage(void);
00050 
00051 int main(int argc, char **argv)
00052 {
00053   nsresult rv;
00054   XRemoteClient client;
00055   char *browser = 0;
00056   char *profile = 0;
00057   char *username = 0;
00058   char *command = 0;
00059 
00060   if (argc < 2) {
00061     print_usage();
00062     return 4;
00063   }
00064 
00065   PLOptStatus os;
00066   PLOptState *opt = PL_CreateOptState(argc, argv, "ha:u:p:");
00067   while (PL_OPT_EOL != (os = PL_GetNextOpt(opt))) {
00068     if (PL_OPT_BAD == os) {
00069       print_usage();
00070       return 4;
00071     }
00072 
00073     switch (opt->option) {
00074     case 'h':
00075       print_usage();
00076       return 4;
00077       break;
00078     case 'u':
00079       username = strdup(opt->value);
00080       break;
00081     case 'a':
00082       browser = strdup(opt->value);
00083       break;
00084     case 'p':
00085       profile = strdup(opt->value);
00086       break;
00087     case 0:
00088       command = strdup(opt->value);      
00089     default:
00090       break;
00091     }
00092   }
00093 
00094   rv = client.Init();
00095   // failed to connect to the X server
00096   if (NS_FAILED(rv))
00097     return 1;
00098 
00099   // send the command - it doesn't get any easier than this
00100   PRBool success = PR_FALSE;
00101   char *error = 0;
00102   rv = client.SendCommand(browser, username, profile, command,
00103                           &error, &success);
00104 
00105   // failed to send command
00106   if (NS_FAILED(rv)) {
00107     fprintf(stderr, "%s: Error: Failed to send command: ", argv[0]);
00108     if (error) {
00109       fprintf(stderr, "%s\n", error);
00110       free(error);
00111     }
00112     else {
00113       fprintf(stderr, "No error string reported..\n");
00114     }
00115 
00116     return 3;
00117   }
00118 
00119   // no running window found
00120   if (!success) {
00121     fprintf(stderr, "%s: Error: Failed to find a running server.\n", argv[0]);
00122     return 2;
00123   }
00124 
00125   // else, everything is fine.
00126   return 0;
00127 }
00128 
00129 /* static */
00130 void print_usage(void) {
00131   fprintf(stderr, "Usage: mozilla-xremote-client [-a firefox|thunderbird|mozilla|any]\n");
00132   fprintf(stderr, "                              [-u <username>]\n");
00133   fprintf(stderr, "                              [-p <profile>] COMMAND\n");
00134 }