aboutsummaryrefslogtreecommitdiffstats
path: root/src/jo_lowbat.cpp
blob: 84905eae85822986435820548fa2702ec6e0fad5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#include <jo_lowbat.hpp>
#include <iostream>
#include <fstream>
#include <cstring>
#include <array>
#include <memory>
#include <algorithm>

using namespace std;

uint8_t
Lowbat::jo_testNotifySend(void) {
	if (system("type notify-send > /dev/null 2>&1")) {
		cerr << "notify-send is not installed. Please install it in order to run lowbat" << endl;
		return (1);
	}
	cout << "notify-send is installed" << endl;
	return (0);
}

uint8_t
Lowbat::jo_testEspeak(void) {
	if (system("type espeak > /dev/null 2>&1")) {
		cerr << "espeak is not installed. Please install it in order to run --say option" << endl;
		return (1);
	}
	cout << "espeak is installed" << endl;
	return (0);
}

int
Lowbat::jo_fetchBatlvl(void) {
	ifstream	bat;

	cout << "Fetching batlvl: ";
	bat.open("/sys/class/power_supply/BAT0/capacity");
	if (!bat.is_open()) {
		cerr << "Failed to open battery info file" << endl;
		exit(JO_RET_OPEN_FAILED);
	}
	getline(bat, m_batlvl);
	bat.close();
	cout << m_batlvl << endl;
	m_batlvl.erase(remove(m_batlvl.begin(), m_batlvl.end(), '\n'), m_batlvl.end());
	cout << m_batlvl << "%" << endl;
	return (stoi(m_batlvl));
}

uint8_t
Lowbat::jo_fetchAcstat(void) {
	ifstream	stat;
	string		l;
	uint8_t		ret;

	cout << "Fetching acstat: ";
	stat.open("/sys/class/power_supply/BAT0/status");
	if (!stat.is_open()) {
		cerr << "Failed to open battery info file" << endl;
		exit(JO_RET_OPEN_FAILED);
	}
	getline(stat, l);
	cout << l << endl;
	if (!strcmp("Discharging", l.c_str())) {
		ret = 0;
	}
	else {
		ret = 1;
	}
	stat.close();
	return (ret);
}

int
Lowbat::jo_notify(void) {
	string		str;

	str = "notify-send \"Low battery: ";
	str += m_batlvl;
	str += "%\" \"Please plug in computer\" -u critical -t 15000";
	cout << "Notifying" << endl;
	return (system(str.c_str()));
}

int
Lowbat::jo_speak(void) {
	string		str;

	str = "echo \"";
	str += m_msg->c_str();
	str += "\" | espeak";
	cout << "Speaking: " << m_msg->c_str() << endl;
	return (system(str.c_str()));
}

void
Lowbat::jo_setMsg(const char *msg) {
	m_msg = new string(msg);
}

void
Lowbat::jo_delMsg(void) {
	delete m_msg;
}