2017-01-13 13:33:16 +00:00
|
|
|
#include <cassert>
|
|
|
|
|
2016-12-19 21:01:37 +00:00
|
|
|
#include "drawtypes/label.hpp"
|
2017-01-13 13:33:16 +00:00
|
|
|
#include "modules/github.hpp"
|
2017-01-27 12:29:10 +00:00
|
|
|
#include "utils/concurrency.hpp"
|
2016-12-19 21:01:37 +00:00
|
|
|
|
|
|
|
#include "modules/meta/base.inl"
|
|
|
|
|
|
|
|
POLYBAR_NS
|
|
|
|
|
|
|
|
namespace modules {
|
|
|
|
template class module<github_module>;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Construct module
|
|
|
|
*/
|
2016-12-21 07:00:09 +00:00
|
|
|
github_module::github_module(const bar_settings& bar, string name_)
|
|
|
|
: timer_module<github_module>(bar, move(name_)), m_http(http_util::make_downloader()) {
|
2016-12-30 22:32:05 +00:00
|
|
|
m_accesstoken = m_conf.get(name(), "token");
|
2016-12-31 02:04:01 +00:00
|
|
|
m_interval = m_conf.get<decltype(m_interval)>(name(), "interval", 60s);
|
2016-12-20 04:53:45 +00:00
|
|
|
m_empty_notifications = m_conf.get(name(), "empty-notifications", m_empty_notifications);
|
2016-12-19 21:01:37 +00:00
|
|
|
|
|
|
|
m_formatter->add(DEFAULT_FORMAT, TAG_LABEL, {TAG_LABEL});
|
|
|
|
|
|
|
|
if (m_formatter->has(TAG_LABEL)) {
|
|
|
|
m_label = load_optional_label(m_conf, name(), TAG_LABEL, "Notifications: %notifications%");
|
2016-12-23 04:12:33 +00:00
|
|
|
m_label->replace_token("%notifications%", m_empty_notifications ? "0" : "");
|
2016-12-19 21:01:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update module contents
|
|
|
|
*/
|
|
|
|
bool github_module::update() {
|
|
|
|
string content{m_http->get("https://api.github.com/notifications?access_token=" + m_accesstoken)};
|
|
|
|
long response_code{m_http->response_code()};
|
|
|
|
|
|
|
|
switch (response_code) {
|
|
|
|
case 200:
|
|
|
|
break;
|
|
|
|
case 401:
|
|
|
|
throw module_error("Bad credentials");
|
|
|
|
case 403:
|
|
|
|
throw module_error("Maximum number of login attempts exceeded");
|
|
|
|
default:
|
|
|
|
throw module_error("Unspecified error (" + to_string(response_code) + ")");
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t pos{0};
|
|
|
|
size_t notifications{0};
|
|
|
|
|
|
|
|
while ((pos = content.find("\"unread\":true", pos + 1)) != string::npos) {
|
|
|
|
notifications++;
|
|
|
|
}
|
|
|
|
|
2017-01-13 13:33:16 +00:00
|
|
|
if (notifications || m_empty_notifications) {
|
2016-12-19 21:01:37 +00:00
|
|
|
m_label->reset_tokens();
|
2017-01-13 13:33:16 +00:00
|
|
|
m_label->replace_token("%notifications%", to_string(notifications));
|
|
|
|
} else {
|
|
|
|
m_label->clear();
|
2016-12-19 21:01:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Build module content
|
|
|
|
*/
|
|
|
|
bool github_module::build(builder* builder, const string& tag) const {
|
|
|
|
if (tag == TAG_LABEL) {
|
|
|
|
builder->node(m_label);
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
POLYBAR_NS_END
|