This repository has been archived by the owner on Mar 23, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
u1st.cpp
174 lines (150 loc) · 4.64 KB
/
u1st.cpp
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/*
u1st 0.1
Utilidad por encima de u1sdtool para conocer el estado de Ubuntu One.
Copyright (C) 2013 Juan Bertinetti <[email protected]>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <iostream>
#include <sstream>
#include <string>
#include <cstdio>
#include <cstdlib>
#include <iomanip>
#include <vector>
using namespace std;
string cmd_output(const char cmd[])
{
//código de http://stackoverflow.com/a/478960
//por waqas
FILE* pipe = popen(cmd, "r");
if (!pipe) return "ERROR";
char buffer[128];
string result = "";
while (!feof(pipe)){
if (fgets(buffer, 128, pipe) != NULL)
result += buffer;
}
pclose(pipe);
return result;
}
string human_readable(unsigned int bytes)
{
//basado en http://bazaar.launchpad.net/~vicox/syspeek/trunk/view/head:/syspeek/helper.py
float bytesf = bytes * 1.0;
//máximo 4294967295 bytes = 4 GiB (por el unsigned int). Con eso debería
//alcanzar para los archivos de Ubuntu One
const char *rep[] = {"bytes", "KiB", "MiB", "GiB"};
int repcount = 4;
stringstream ss;
for (int i=0; i<repcount; ++i){
if (bytesf < 1024.0){
ss<<setprecision(3)<<bytesf<<" "<<rep[i];
return ss.str();
}
bytesf /= 1024.0;
}
return "";
}
vector<string> parsear(string &datos, const char *what)
{
stringstream data(datos);
string l;
getline(data, l);
vector<string> v;
if (what == "Descargas"){
if (l == "Current downloads: 0")
return v;
}
else if (what == "Subidas"){
if (l == "Current uploads: 0")
return v;
}
while (getline(data, l)){
stringstream res;
string archivo = l.substr(l.find(":") + 2);
res<<" - "<<archivo<<endl;
getline(data, l);
stringstream s_bytestotal(l.substr(l.find(":") + 1));
unsigned int bytestotal; s_bytestotal>>bytestotal;
getline(data, l);
stringstream s_bytes(l.substr(l.find(":") + 1));
unsigned int bytes; s_bytes>>bytes;
float porc = bytes*100.0/bytestotal;
res<<" "<<human_readable(bytes)<<" de "<<human_readable(bytestotal)<<" ("<<setprecision(3)<<porc<<"%)\n";
v.push_back(res.str());
}
return v;
}
string parsear_estado(string &s)
{
stringstream data(s);
string l, res = "";
while (getline(data, l)){
if (l.find("is_connected:") != string::npos){
res += "Conexión: ";
if (l.find("True") != string::npos)
res += "Conectado\n";
else
res += "No conectado\n";
}
else if (l.find("queues:") != string::npos){
res += "Estado: ";
if (l.find("WORKING") != string::npos)
res += "Trabajando\n";
else if (l.find("IDLE") != string::npos)
res += "Sincronizado\n";
}
}
return res;
}
void error_salir()
{
cout<<"Error ejecutando u1sdtool. El cliente de Ubuntu One puede no\
estar instalado.\n";
abort();
}
int main(){
string out = cmd_output("u1sdtool --status 2>&1");
if (out == "ERROR"){
error_salir();
}
cout<<parsear_estado(out)<<endl;
out = cmd_output("u1sdtool --current-transfers 2>&1");
if (out == "ERROR"){
error_salir();
}
int a = out.find("Current uploads:");
int b = out.find("Current downloads:", a);
string s = out.substr(a, b-a);
vector<string> ups = parsear(s, "Subidas");
cout<<"Subidas";
if (ups.empty())
cout<<":\nNo hay subidas actualmente.\n";
else {
cout<<" ("<<ups.size()<<"):\n";
for (int i=0; i<ups.size(); ++i){
cout<<ups[i];
}
}
s = out.substr(b);
vector<string> downs = parsear(s, "Descargas");
cout<<"\nDescargas";
if (downs.empty())
cout<<":\nNo hay descargas actualmente.\n";
else {
cout<<" ("<<downs.size()<<"):\n";
for (int i=0; i<downs.size(); ++i){
cout<<downs[i];
}
}
return 0;
}