11#include < memory>
2- #include < fstream>
3- #include < iostream>
42#include < glibmm.h>
53
6- #include " gtkmm/enums.h"
74#include " lockergrid.hpp"
85#include " weather.hpp"
96#include " timedrevealer.hpp"
10- #include " wayfire/nonstd/json.hpp"
117
12-
13- void WayfireLockerWeatherPlugin::update_labels (std::string text)
14- {
15- for (auto & it : weather_widgets)
16- {
17- it.second ->label .set_markup (text);
18- }
19-
20- label_contents = text;
21- }
22-
23- void WayfireLockerWeatherPlugin::update_icons (std::string path)
24- {
25- for (auto & it : weather_widgets)
26- {
27- it.second ->image .set (path);
28- }
29-
30- icon_path = path;
31- }
32-
33- WayfireLockerWeatherPluginWidget::WayfireLockerWeatherPluginWidget (std::string contents,
34- std::string icon_path) :
8+ WayfireLockerWeatherPluginWidget::WayfireLockerWeatherPluginWidget () :
359 WayfireLockerTimedRevealer(" locker/weather_always" )
3610{
37- set_child (box);
38- label.add_css_class (" weather" );
39- label.set_markup (contents);
40- label.set_justify (Gtk::Justification::CENTER );
41- box.prepend (label);
42-
43- image.add_css_class (" weather" );
44- image.set (icon_path);
45- box.append (image);
11+ set_child (weather);
12+ weather.add_css_class (" weather" );
4613}
4714
4815void WayfireLockerWeatherPlugin::add_output (std::string id, std::shared_ptr<WayfireLockerGrid> grid)
4916{
50- weather_widgets.emplace (id, new WayfireLockerWeatherPluginWidget (label_contents, icon_path ));
17+ weather_widgets.emplace (id, new WayfireLockerWeatherPluginWidget ());
5118
5219 auto weather_widget = weather_widgets[id];
5320 if (!shown)
@@ -64,86 +31,10 @@ void WayfireLockerWeatherPlugin::remove_output(std::string id, std::shared_ptr<W
6431 weather_widgets.erase (id);
6532}
6633
67- bool WayfireLockerWeatherPlugin::handle_inotify_event (Glib::IOCondition cond)
68- {
69- if (cond == Glib::IOCondition::IO_HUP )
70- {
71- return false ;
72- }
73-
74- char buf[1024 * sizeof (inotify_event)];
75- read (inotify_fd, buf, sizeof (buf));
76-
77- update_weather ();
78-
79- return true ;
80- }
81-
82- void WayfireLockerWeatherPlugin::update_weather ()
83- {
84- std::ifstream input_file (weather_data_path);
85-
86- if (!input_file)
87- {
88- std::cerr << " Error reading json file " << weather_data_path << std::endl;
89- hide ();
90- return ;
91- }
92-
93- std::stringstream buf;
94- buf << input_file.rdbuf ();
95-
96- wf::json_t json_data;
97-
98- auto err = wf::json_t::parse_string (buf.str (), json_data);
99-
100- if (err.has_value ())
101- {
102- std::cerr << " Error parsing json data " << weather_data_path << " : " << *err << std::endl;
103- hide ();
104- return ;
105- }
106-
107- if (!json_data.has_member (" temp" ) || !json_data.has_member (" icon" ))
108- {
109- std::cerr << " Unexpected weather json data in " << weather_data_path << std::endl;
110- hide ();
111- return ;
112- }
113-
114- update_labels (json_data[" temp" ]);
115- update_icons (json_data[" icon" ]);
116-
117- show ();
118- }
119-
12034WayfireLockerWeatherPlugin::WayfireLockerWeatherPlugin () :
12135 WayfireLockerPlugin(" locker/weather" )
12236{}
12337
124- void WayfireLockerWeatherPlugin::init ()
125- {
126- weather_data_path = std::string (getenv (" HOME" )) + " /.local/share/owf/data/data.json" ;
127-
128- inotify_fd = inotify_init ();
129-
130- inotify_add_watch (inotify_fd,
131- weather_data_path.c_str (),
132- IN_CLOSE_WRITE );
133-
134- inotify_connection = Glib::signal_io ().connect (
135- sigc::mem_fun (*this , &WayfireLockerWeatherPlugin::handle_inotify_event),
136- inotify_fd, Glib::IOCondition::IO_IN | Glib::IOCondition::IO_HUP );
137-
138- update_weather ();
139- }
140-
141- void WayfireLockerWeatherPlugin::deinit ()
142- {
143- inotify_connection.disconnect ();
144- close (inotify_fd);
145- }
146-
14738void WayfireLockerWeatherPlugin::hide ()
14839{
14940 for (auto & it : weather_widgets)
0 commit comments