Switch downloads to be done asynchronously, use libsoup-2.4 instead of libcurl
[beifahrer] / src / beifahrer-cli.vala
1 /* This file is part of Beifahrer.
2  *
3  * Copyright (C) 2010 Philipp Zabel
4  *
5  * Beifahrer is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * Beifahrer 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
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with Beifahrer. If not, see <http://www.gnu.org/licenses/>.
17  */
18
19 AdacMitfahrclub adac;
20 MainLoop loop;
21
22 async void get_lifts (string city_from, string city_to, Date date) {
23         print ("Lifts from %s to %s on %d.%d.%d\n", city_from, city_to,
24                date.get_day (), date.get_month (), date.get_year ());
25
26         var lift_list = yield adac.get_lift_list (city_from, city_to, date);
27         foreach (Lift lift in lift_list) {
28                 print ("%s\t%s\t%s\t%s\t%d\t%s\t", lift.city_from, lift.city_to, lift.date, lift.time, lift.places, lift.price);
29                 if (LiftFlags.SMOKER in lift.flags)
30                         print ("smoker ");
31                 else if (LiftFlags.NON_SMOKER in lift.flags)
32                         print ("non_smoker ");
33                 if (LiftFlags.ADAC_MEMBER in lift.flags)
34                         print ("adac ");
35                 if  (LiftFlags.WOMEN_ONLY in lift.flags)
36                         print ("women ");
37                 print ("\n");
38                 print ("%s\n", lift.href);
39         }
40
41         loop.quit ();
42 }
43
44 async void get_details (string href) {
45         var lift = new Lift ();
46         lift.href = href;
47         yield adac.update_lift_details (lift);
48
49         print ("%s\t%s\t%s\t%s\t%d\t%s\t", lift.city_from, lift.city_to, lift.date, lift.time, lift.places, lift.price);
50         if (LiftFlags.SMOKER in lift.flags)
51                 print ("smoker ");
52         else if (LiftFlags.NON_SMOKER in lift.flags)
53                 print ("non_smoker ");
54         if (LiftFlags.ADAC_MEMBER in lift.flags)
55                 print ("adac ");
56         if  (LiftFlags.WOMEN_ONLY in lift.flags)
57                 print ("women ");
58                print ("\n");
59         foreach (string via in lift.city_via) {
60                 print ("\tvia %s\n", via);
61         }
62
63         print ("Driver: %s (%s)\n", lift.name, lift.phone);
64         print ("Description:\n%s\n", lift.description);
65
66         loop.quit ();
67 }
68
69 static int main (string[] args) {
70         loop = new MainLoop (null);
71
72         if (args.length < 3) {
73                 print ("usage: beifahrer-cli <city_from> <city_to> [date]\n");
74                 print ("or:    beifahrer-cli details <lift_url>\n");
75                 return 1;
76         }
77
78         adac = new AdacMitfahrclub ();
79
80         if (args[1] == "details") {
81                 get_details.begin (args[2]);
82
83                 loop.run ();
84                 return 0;
85         }
86
87         string city_from = args[1];
88         string city_to = args[2];
89
90         var date = Date ();
91         if (args.length > 3) {
92                 date.set_parse (args[3]);
93         } else {
94                 var now = TimeVal ();
95                 date.set_time_val (now);
96         }
97
98         get_lifts.begin (city_from, city_to, date);
99
100         loop.run ();
101         return 0;
102 }