Skip to content

Commit c1b3e79

Browse files
committed
works without background thread
1 parent 0d22eb5 commit c1b3e79

File tree

5 files changed

+202
-24
lines changed

5 files changed

+202
-24
lines changed

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,10 @@ Run `cmake` to configure the build environment and then `make` to build
1515
To install, use `make install`
1616

1717
sudo make install
18+
19+
Get secret key from [darksky.net](https://darksky.net/dev/account) and store it in your config file.
20+
21+
cat ~/.weather.config
22+
{
23+
"key":"XXXXXXXXXXXXXXXXXXXX"
24+
}

src/CMakeLists.txt

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
find_package (PkgConfig)
22

33
# Add all your dependencies to the list below
4-
pkg_check_modules (DEPS REQUIRED gthread-2.0 gtk+-3.0 gio-2.0 wingpanel-2.0 granite)
4+
pkg_check_modules (DEPS REQUIRED gthread-2.0 gtk+-3.0 gio-2.0 wingpanel-2.0 granite libsoup-2.4 json-glib-1.0)
55

66
add_definitions (${DEPS_CFLAGS})
77
link_directories (${DEPS_LIBRARY_DIRS})
@@ -14,9 +14,13 @@ include (ValaPrecompile)
1414
# Add all your vala files and requires packages to the List below to include them in the build
1515
vala_precompile (VALA_C ${CMAKE_PROJECT_NAME}
1616
Indicator.vala
17+
Widgets/DisplayWidget.vala
18+
lib/Weather.vala
1719
${CMAKE_CURRENT_BINARY_DIR}/config.vala
1820
PACKAGES
19-
gio-2.0
21+
libsoup-2.4
22+
gio-2.0
23+
json-glib-1.0
2024
gtk+-3.0
2125
wingpanel-2.0
2226
granite

src/Indicator.vala

Lines changed: 51 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -18,55 +18,84 @@
1818
*/
1919

2020
public class Weather.Indicator : Wingpanel.Indicator {
21-
private const string ICON_NAME = "weather-application";
21+
Weather.Widgets.DisplayWidget? display_widget = null;
2222

2323
private Wingpanel.IndicatorManager.ServerType server_type;
24-
private Wingpanel.Widgets.OverlayIcon indicator_icon;
25-
2624

2725
private const string WEATHER_APP = "Weather App";
2826

2927
private Gtk.Grid main_grid;
28+
29+
private File config;
30+
31+
private string key;
3032

3133
public Indicator (Wingpanel.IndicatorManager.ServerType server_type) {
3234
Object (code_name: WEATHER_APP,
3335
display_name: _("Weather"),
3436
description: _("The Weather indicator"));
3537

38+
try {
39+
config = File.new_for_path (Environment.get_home_dir() + "/.weather.config");
40+
stderr.printf("home dir: %s\n",Environment.get_home_dir());
41+
42+
if (config.query_exists ()) {
43+
44+
var parser = new Json.Parser ();
45+
var dis = new DataInputStream (config.read ());
46+
47+
parser.load_from_stream(dis,null);
48+
49+
var root_object = parser.get_root ().get_object ();
50+
key = root_object.get_string_member ("key");
51+
}
52+
else {
53+
stderr.printf("config file NOT FOUND!\n");
54+
}
55+
56+
}catch (Error e) {
57+
stderr.printf ("Error: %s\n", e.message);
58+
return;
59+
60+
}
61+
3662
this.server_type = server_type;
3763
}
3864

3965
public override Gtk.Widget get_display_widget () {
40-
if (indicator_icon == null) {
41-
indicator_icon = new Wingpanel.Widgets.OverlayIcon (ICON_NAME);
42-
indicator_icon.button_press_event.connect ((e) => {
43-
if (e.button == Gdk.BUTTON_MIDDLE) {
44-
close ();
45-
return Gdk.EVENT_STOP;
46-
}
47-
48-
return Gdk.EVENT_PROPAGATE;
49-
});
66+
67+
if (display_widget == null) {
68+
display_widget = new Widgets.DisplayWidget ();
5069
}
5170

52-
return indicator_icon;
71+
visible = true;
72+
73+
return display_widget;
5374
}
5475

5576
public override Gtk.Widget? get_widget () {
56-
if (main_grid == null) {
57-
debug ("grid is null");
58-
}
59-
77+
//TODO: popup to show details like location, temp and discriptive forcast.
6078
this.visible = true;
61-
62-
return main_grid;
79+
80+
monitor_weather.begin((obj, res)=> {
81+
monitor_weather.end(res);
82+
});
83+
84+
return null;
6385
}
6486

65-
87+
private async void monitor_weather() {
88+
89+
var result = get_weather("",key);
90+
display_widget.update_state(result.short_discription,result.temperature);
91+
92+
}
6693

6794
public void connections () {}
6895

69-
public override void opened () {}
96+
public override void opened () {
97+
98+
}
7099

71100
public override void closed () {}
72101

src/Widgets/DisplayWidget.vala

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
2+
public class Weather.Widgets.DisplayWidget : Gtk.Box {
3+
private Gtk.Image image;
4+
private Gtk.Label degree;
5+
6+
public DisplayWidget () {
7+
Object (orientation: Gtk.Orientation.HORIZONTAL);
8+
}
9+
10+
construct {
11+
image = new Gtk.Image ();
12+
//TODO: find a better icon for weather Plugin.
13+
image.icon_name = "weather-clear-symbolic";
14+
image.icon_size = Gtk.IconSize.SMALL_TOOLBAR;
15+
degree = new Gtk.Label ("");
16+
pack_start (image);
17+
pack_start (degree);
18+
}
19+
20+
21+
public void update_state (string state, double temperature) {
22+
23+
// Convert the F to C and copy in label.
24+
var degreeTemp = (temperature - 32.0)*(0.55);
25+
char[] buf = new char[double.DTOSTR_BUF_SIZE];
26+
unowned string str = degreeTemp.format (buf, "%2.2g");
27+
degree.label = str + "\u00b0C";
28+
29+
switch (state) {
30+
case "Clear":
31+
image.icon_name = "weather-clear-symbolic";
32+
break;
33+
case "Rainy":
34+
image.icon_name = "weather-showers-symbolic";
35+
break;
36+
case "Snow":
37+
image.icon_name = "weather-snow-symbolic";
38+
break;
39+
case "Cloudy":
40+
case "Mostly Cloudy":
41+
image.icon_name = "weather-few-clouds-symbolic";
42+
break;
43+
case "Overcast":
44+
image.icon_name = "weather-overcast-symbolic";
45+
break;
46+
case "Breezy":
47+
image.icon_name = "weather-windy-symbolic";
48+
break;
49+
default:
50+
//TODO: Find better icon (\ over weather icon.')
51+
image.icon_name = "weather-clear-symbolic";
52+
53+
break;
54+
}
55+
}
56+
}

src/lib/Weather.vala

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
using Soup;
2+
using Json;
3+
4+
struct weatherInfo{
5+
string short_discription;
6+
double temperature;
7+
}
8+
9+
string get_ip(Soup.Session session) {
10+
string uri = "https://api.ipify.org";
11+
var message = new Soup.Message("GET", uri);
12+
session.send_message(message);
13+
14+
return (string)message.response_body.data;
15+
16+
17+
}
18+
19+
weatherInfo get_weather(string location, string secret_key) {
20+
21+
//Init the session.
22+
var session = new Soup.Session ();
23+
var info = new weatherInfo();
24+
25+
string loc_uri = "http://freegeoip.net/json/" + get_ip(session);
26+
27+
string uri = "https://api.darksky.net/forecast/" + secret_key + "/";
28+
29+
30+
31+
// Get Location.
32+
double lat, lon;
33+
var query_location = new Soup.Message ("GET", loc_uri);
34+
session.send_message (query_location);
35+
36+
try {
37+
var parser = new Json.Parser();
38+
39+
parser.load_from_data((string) query_location.response_body.flatten().data, -1);
40+
41+
var root_object = parser.get_root().get_object();
42+
lat = root_object.get_double_member("latitude");
43+
lon = root_object.get_double_member("longitude");
44+
var city = root_object.get_string_member("city");
45+
46+
stderr.printf("Location : lat : %g, lon : %g \n", lat, lon);
47+
stderr.printf("Location : City: %s \n", city);
48+
49+
}catch (Error e){
50+
stderr.printf(" Unable to get location\n");
51+
return info ;
52+
}
53+
54+
55+
56+
var weather_uri = uri + lat.to_string() + "," + lon.to_string();
57+
var message = new Soup.Message ("GET", weather_uri);
58+
session.send_message (message);
59+
60+
61+
try {
62+
var parser = new Json.Parser ();
63+
64+
parser.load_from_data ((string) message.response_body.flatten().data, -1);
65+
66+
var root_object = parser.get_root ().get_object ();
67+
var currently = root_object.get_object_member ("currently");
68+
var summary = currently.get_string_member ("summary");
69+
var temp = currently.get_double_member("temperature");
70+
71+
stderr.printf("current : %s\n", summary);
72+
info.short_discription = summary;
73+
info.temperature = temp;
74+
75+
76+
} catch (Error e) {
77+
stderr.printf ("I guess something is not working... %s \n", e.message);
78+
}
79+
80+
return info;
81+
82+
}

0 commit comments

Comments
 (0)