Skip to content

Commit f1b4be6

Browse files
committed
migrate locker to use new weather helper
1 parent 44a75e9 commit f1b4be6

2 files changed

Lines changed: 13 additions & 136 deletions

File tree

‎src/locker/plugin/weather.cpp‎

Lines changed: 4 additions & 113 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,20 @@
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

4815
void 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-
12034
WayfireLockerWeatherPlugin::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-
14738
void WayfireLockerWeatherPlugin::hide()
14839
{
14940
for (auto& it : weather_widgets)

‎src/locker/plugin/weather.hpp‎

Lines changed: 9 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,17 @@
11
#pragma once
2-
#include <sys/inotify.h>
3-
#include <gtkmm/label.h>
4-
#include <gtkmm/box.h>
5-
#include <gtkmm/image.h>
62
#include <unordered_map>
73

84
#include "plugin.hpp"
95
#include "timedrevealer.hpp"
10-
#include "wf-option-wrap.hpp"
116
#include "lockergrid.hpp"
127

8+
#include "widget-utils/weather.hpp"
9+
1310
class WayfireLockerWeatherPluginWidget : public WayfireLockerTimedRevealer
1411
{
1512
public:
16-
Gtk::Box box;
17-
Gtk::Label label;
18-
Gtk::Image image;
19-
WayfireLockerWeatherPluginWidget(std::string contents, std::string icon_path);
13+
ShellWeather weather;
14+
WayfireLockerWeatherPluginWidget();
2015
};
2116

2217
class WayfireLockerWeatherPlugin : public WayfireLockerPlugin
@@ -25,23 +20,14 @@ class WayfireLockerWeatherPlugin : public WayfireLockerPlugin
2520
WayfireLockerWeatherPlugin();
2621
void add_output(std::string id, std::shared_ptr<WayfireLockerGrid> grid) override;
2722
void remove_output(std::string id, std::shared_ptr<WayfireLockerGrid> grid) override;
28-
void init() override;
29-
void deinit() override;
30-
31-
32-
int inotify_fd;
33-
sigc::connection inotify_connection;
34-
std::string weather_data_path;
35-
bool handle_inotify_event(Glib::IOCondition cond);
36-
void update_weather();
37-
void update_labels(std::string text);
38-
void update_icons(std::string path);
23+
void init() override
24+
{}
25+
void deinit() override
26+
{}
3927

4028
void hide();
4129
void show();
4230

4331
std::unordered_map<std::string, std::shared_ptr<WayfireLockerWeatherPluginWidget>> weather_widgets;
44-
std::string label_contents = "";
45-
std::string icon_path = "";
46-
bool shown;
32+
bool shown = true;
4733
};

0 commit comments

Comments
 (0)