~gwibber-committers/gwibber/libgwibber

16 by Ken VanDine
consolidated examples into a single source
1
/*
2
 * Copyright (C) 2010 Ken VanDine
3
 *
4
 * This library is free software; you can redistribute it and/or modify
5
 * it under the terms of the GNU Lesser General Public License
6
 * version 3.0 as published by the Free Software Foundation.
7
 *
8
 * This library is distributed in the hope that it will be useful,
9
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
 * GNU Lesser General Public License version 3.0 for more details.
12
 *
13
 * You should have received a copy of the GNU Lesser General Public
14
 * License along with this library. If not, see
15
 * <http://www.gnu.org/licenses/>.
16
 *
17
 * Authored by Ken VanDine <ken.vandine@canonical.com>
18
 */
19
20
static int main(string[] args) {
17 by Ken VanDine
Experimenting with json-glib, i think it is broken
21
/*
16 by Ken VanDine
consolidated examples into a single source
22
    if (args.length < 2) {
23
        PrintUsage ();
24
        return 1;
25
    }
17 by Ken VanDine
Experimenting with json-glib, i think it is broken
26
*/
16 by Ken VanDine
consolidated examples into a single source
27
    var service = new Gwibber.Service();
41 by Ken VanDine
refactored to move Accounts out of Service
28
    var accounts = new Gwibber.Accounts();
77.1.3 by Ken VanDine
Added is_available signal for Streams, Connection, Accounts, and URLShorten
29
    var streams = new Gwibber.Streams();
16 by Ken VanDine
consolidated examples into a single source
30
31
    switch (args[1]) {
32
        case "--refresh":
33
            stdout.printf("Refreshing gwibber\n");
34
            service.refresh();
35
            return 0;
75 by Ken VanDine
* Added Gwibber.Service.start method for starting gwibber-service
36
        case "--start":
37
            stdout.printf("Starting the gwibber service\n");
38
            service.start();
39
            return 0;
40
        case "--shutdown":
41
            stdout.printf("Shutting down the gwibber service\n");
17 by Ken VanDine
Experimenting with json-glib, i think it is broken
42
            service.quit();
43
            return 0;
43 by Ken VanDine
* Added missing example file
44
        case "--version":
45
            string version = service.version();
16 by Ken VanDine
consolidated examples into a single source
46
            stdout.printf("Gwibber version is: %s\n", version);
47
            return 0;
77.1.3 by Ken VanDine
Added is_available signal for Streams, Connection, Accounts, and URLShorten
48
	/*
49
        case "--messages":
50
            string messages = streams.messages();
51
            stdout.printf("Gwibber messages: %s\n", messages);
52
            return 0;
53
	*/
43 by Ken VanDine
* Added missing example file
54
        case "--accounts":
93 by Ken VanDine
Work toward being more "GObject" friendly, using properties for Account
55
            GLib.List<Gwibber.Account> accounts_list = accounts.list ();
56
            foreach (Gwibber.Account account in accounts_list) {
57
                GLib.Value? value = null;
58
                stdout.printf("ID is %s\n", account.id);
59
                stdout.printf("username is %s\n", account.username);
60
                stdout.printf("color is %s\n", account.color);
61
                stdout.printf("service is %s\n", account.service);
98.1.2 by Ken VanDine
Use strings to check for {send,receive}_enabled, so we can default to null
62
                stdout.printf ("\tsend_enabled: %s\n", (account.send_enabled == "1")?"true":"false");
63
                stdout.printf ("\treceive_enabled: %s\n", (account.receive_enabled == "1")?"true":"false");
28 by Ken VanDine
more twiddling with json parsing for GetAccounts
64
            }
17 by Ken VanDine
Experimenting with json-glib, i think it is broken
65
            return 0;
43 by Ken VanDine
* Added missing example file
66
        case "--features":
67
            HashTable<string,HashTable> features_table = service.features ();
32 by Ken VanDine
fix some json handling for GetServices
68
            GLib.List<string> k = features_table.get_keys();
30 by Ken VanDine
better handling of features results
69
            foreach (string s in k) {
70
                stdout.printf ("Feature: %s\n",s);
32 by Ken VanDine
fix some json handling for GetServices
71
                HashTable<string,Value?> feature_table = features_table.lookup(s);
72
                GLib.List<string> kk = feature_table.get_keys();
73
                foreach (string ss in kk) {
74
                    if (feature_table.lookup(ss).type_name() == "gboolean")
75
                       stdout.printf ("\t%s: %s\n", ss, (feature_table.lookup(ss).get_boolean())?"true":"false");
76
                    else if (feature_table.lookup(ss).type_name() == "gchararray")
77
                       stdout.printf ("\t%s: %s\n", ss, (string)feature_table.lookup(ss));
78
                }
79
            }
80
            return 0;
45 by Ken VanDine
Simplify method names for services
81
        case "--services":
82
            HashTable<string,HashTable> services_table = service.services ();
32 by Ken VanDine
fix some json handling for GetServices
83
            GLib.List<string> k = services_table.get_keys();
84
            foreach (string s in k) {
85
                stdout.printf ("Service: %s\n",s);
86
                HashTable<string,Value?> service_table = services_table.lookup(s);
87
                GLib.List<string> kk = service_table.get_keys();
88
                foreach (string ss in kk) {
89
                    if (service_table.lookup(ss).type_name() == "GHashTable") {
90
                        HashTable<string,Value?> service_attr_table = (HashTable)service_table.lookup(ss);
91
                        if (service_attr_table != null) {
92
                            GLib.List<string> kkk = service_attr_table.get_keys();
93
                            foreach (string sss in kkk) {
94
                                if (service_attr_table.lookup(ss).type_name() == "gboolean")
95
                                   stdout.printf ("\t%s: %s\n", sss, (service_attr_table.lookup(ss).get_boolean())?"true":"false");
96
                                else if (service_attr_table.lookup(ss).type_name() == "gchararray")
97
                                   stdout.printf ("\t%s: %s\n", sss, (string)service_attr_table.lookup(ss));
98
                            }
99
                        continue;
100
                        }
101
                    } else if (service_table.lookup(ss).type_name() == "gboolean")
102
                       stdout.printf ("\t%s: %s\n", ss, (service_table.lookup(ss).get_boolean())?"true":"false");
103
                    else if (service_table.lookup(ss).type_name() == "gchararray")
104
                       stdout.printf ("\t%s: %s\n", ss, (string)service_table.lookup(ss));
105
                }
106
            }
107
            return 0;
108
16 by Ken VanDine
consolidated examples into a single source
109
        case "--send-message":
110
            if (args.length < 3) {
111
                PrintUsage ();
112
                return 1;
113
            }
114
            for(int i=1; i<args.length; i++) {
115
                if (args[i] == "--send-message") 
116
                    if (args[1+1] != null) {
117
                        string message = args[i+1];
118
                        stdout.printf("Posting...\n");
33 by Ken VanDine
Rename functions to be more inline with C and vala standards
119
                        service.send_message(message);
16 by Ken VanDine
consolidated examples into a single source
120
                    } else {
121
                        PrintUsage ();
122
                        return 1;
123
                    }
124
            }
125
            return 0;
126
        case "--help":
127
            PrintUsage ();
128
            return 0;
129
130
        default:
131
        
132
            return 1;
133
    }
134
}
135
136
private static void PrintUsage () {
137
    string usage = "Usage:\n" +
138
        "  --help\t\t\tPrint this usage message.\n" +
75 by Ken VanDine
* Added Gwibber.Service.start method for starting gwibber-service
139
        "  --start\t\t\tStart the gwibber-service\n" +
140
        "  --shutdown\t\t\tShutdown the gwibber-service\n" +
16 by Ken VanDine
consolidated examples into a single source
141
        "  --refresh\t\t\tRefresh the gwibber-service\n" +
142
        "  --send-message [message]\t\tMessage to post\n" +
83 by Ken VanDine
Added help output for services, accounts and features to example-vala
143
        "  --accounts\t\t\tReturns list of accounts\n" +
144
        "  --services\t\t\tReturns list of services\n" +
145
        "  --features\t\t\tReturns list of features\n" +
45 by Ken VanDine
Simplify method names for services
146
        "  --version\t\t\tReturns the version of the gwibber-service\n";
16 by Ken VanDine
consolidated examples into a single source
147
        stdout.printf ("%s\n".printf(usage));
148
}