| 1 | /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */ |
|---|
| 2 | /* |
|---|
| 3 | * Copyright (C) 2000 Eazel, Inc |
|---|
| 4 | * |
|---|
| 5 | * This program is free software; you can redistribute it and/or |
|---|
| 6 | * modify it under the terms of the GNU General Public License as |
|---|
| 7 | * published by the Free Software Foundation; either version 2 of the |
|---|
| 8 | * License, or (at your option) any later version. |
|---|
| 9 | * |
|---|
| 10 | * This program is distributed in the hope that it will be useful, |
|---|
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|---|
| 13 | * General Public License for more details. |
|---|
| 14 | * |
|---|
| 15 | * You should have received a copy of the GNU General Public |
|---|
| 16 | * License along with this program; if not, write to the |
|---|
| 17 | * Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
|---|
| 18 | * Boston, MA 02111-1307, USA. |
|---|
| 19 | * |
|---|
| 20 | * Authors: Ian McKellar <yakk@yakk.net.au> |
|---|
| 21 | * |
|---|
| 22 | */ |
|---|
| 23 | |
|---|
| 24 | #include <config.h> |
|---|
| 25 | #include <glib.h> |
|---|
| 26 | #include <popt-gnome.h> |
|---|
| 27 | #include <string.h> |
|---|
| 28 | #include <stdlib.h> |
|---|
| 29 | #include "vault-operations.h" |
|---|
| 30 | |
|---|
| 31 | #define _(X) X // eek! FIXME bugzilla.eazel.com 2591 |
|---|
| 32 | |
|---|
| 33 | gchar *vault_location; |
|---|
| 34 | gboolean debug = FALSE; |
|---|
| 35 | gchar *operation = NULL; |
|---|
| 36 | |
|---|
| 37 | static const struct poptOption options[] = { |
|---|
| 38 | {"debug", 'd', POPT_ARG_NONE, &debug, 0, _("Enable debugging"), NULL}, |
|---|
| 39 | {"uri", 'u', POPT_ARG_STRING, &vault_location, 0, _("Vault location"), NULL}, |
|---|
| 40 | {NULL, '\0', 0, NULL, 0} /* end the list */ |
|---|
| 41 | }; |
|---|
| 42 | |
|---|
| 43 | static void valid_ops() { |
|---|
| 44 | gint vopnum = 0; |
|---|
| 45 | struct VaultOperation *vop; |
|---|
| 46 | |
|---|
| 47 | g_print(_("Valid operations:")); |
|---|
| 48 | for(;;) { |
|---|
| 49 | vop = &vault_operations[vopnum++]; |
|---|
| 50 | if(vop->name == NULL) { |
|---|
| 51 | g_print(".\n"); |
|---|
| 52 | return; |
|---|
| 53 | } |
|---|
| 54 | g_print(" %s", vop->name); |
|---|
| 55 | } |
|---|
| 56 | } |
|---|
| 57 | |
|---|
| 58 | int main (int argc, char *argv[]) { |
|---|
| 59 | poptContext pctx = poptGetContext("eazel-vault", argc, argv, |
|---|
| 60 | options, 0); |
|---|
| 61 | gint opt; |
|---|
| 62 | gint vopnum = 0; |
|---|
| 63 | struct VaultOperation *vop; |
|---|
| 64 | |
|---|
| 65 | vault_location = g_strdup("http://localhost/webdav/"); /* load from gconf */ |
|---|
| 66 | |
|---|
| 67 | while ( (opt = poptGetNextOpt (pctx)) >= 0) { |
|---|
| 68 | switch (opt) { |
|---|
| 69 | case 'd': |
|---|
| 70 | debug = TRUE; |
|---|
| 71 | break; |
|---|
| 72 | case 'u': |
|---|
| 73 | g_free(vault_location); |
|---|
| 74 | vault_location = g_strdup(poptGetArg(pctx)); |
|---|
| 75 | } |
|---|
| 76 | } |
|---|
| 77 | |
|---|
| 78 | operation = g_strdup(poptGetArg(pctx)); |
|---|
| 79 | |
|---|
| 80 | #if 0 |
|---|
| 81 | g_print("vault_location = `%s'\n", vault_location); |
|---|
| 82 | g_print("debug = `%d'\n", debug); |
|---|
| 83 | g_print("operation = `%s'\n", operation); |
|---|
| 84 | #endif |
|---|
| 85 | |
|---|
| 86 | if(operation == NULL) { |
|---|
| 87 | g_print(_("Error: No operation supplied\n")); |
|---|
| 88 | valid_ops(); |
|---|
| 89 | exit(1); |
|---|
| 90 | } |
|---|
| 91 | |
|---|
| 92 | for(;;) { |
|---|
| 93 | vop = &vault_operations[vopnum++]; |
|---|
| 94 | if(vop->name == NULL) { |
|---|
| 95 | g_print(_("Error: Invalid operation supplied (%s)\n"), operation); |
|---|
| 96 | valid_ops(); |
|---|
| 97 | exit(1); |
|---|
| 98 | } |
|---|
| 99 | if(!g_strcasecmp(vop->name, operation)) { |
|---|
| 100 | /* we've found a matching operation */ |
|---|
| 101 | GList *args = NULL; |
|---|
| 102 | gint argcount = 0; |
|---|
| 103 | gchar *arg; |
|---|
| 104 | while((arg = poptGetArg(pctx))) { |
|---|
| 105 | argcount++; |
|---|
| 106 | args = g_list_append(args, arg); |
|---|
| 107 | } |
|---|
| 108 | if(argcount <= vop->maxargs && argcount >= vop->minargs) { |
|---|
| 109 | GnomeVFSResult result; |
|---|
| 110 | gchar *error_context = "eazel-vault"; |
|---|
| 111 | |
|---|
| 112 | poptFreeContext (pctx); |
|---|
| 113 | gnome_vfs_init (); |
|---|
| 114 | result = (vop->function)(args, vault_location, debug, &error_context); |
|---|
| 115 | if(result != GNOME_VFS_OK) { |
|---|
| 116 | g_print("%s: %s\n", error_context, gnome_vfs_result_to_string(result)); |
|---|
| 117 | } |
|---|
| 118 | return result; |
|---|
| 119 | } else { |
|---|
| 120 | g_print(_("Error: Invalid syntax\nSyntax: %s\n"), vop->syntax); |
|---|
| 121 | exit(1); |
|---|
| 122 | } |
|---|
| 123 | |
|---|
| 124 | } |
|---|
| 125 | |
|---|
| 126 | } |
|---|
| 127 | |
|---|
| 128 | return 0; |
|---|
| 129 | }; |
|---|