188
189
/** use the get_gtk_item function to get an icon theme, or use hicolor if nothing is found*/
189
std::string get_gtk_icon_theme(){return get_gtk_item("icon","hicolor");}
190
std::string get_gtk_icon_theme(){return get_gtk_themeitem("icon","hicolor");}
190
191
/** get the gtk theme using the get_gtk_item function, or return Raleigh if nothing is found*/
191
std::string get_gtk_widget_theme(){return get_gtk_item("gtk","Raleigh");}
192
std::string get_gtk_widget_theme(){return get_gtk_themeitem("gtk","Raleigh");}
192
193
/** get something from org.gnome.desktop.interface *-theme
193
194
* @param itemToGet this will be icon OR gtk only
194
195
* @param defaultTheme the default theme to return
196
std::string get_gtk_item(std::string itemToGet, std::string defaultTheme){
197
std::string get_gtk_item(std::string itemToGet, std::string defaultItem){
198
std::string temp=sed_i(itemToGet,"-","_");
199
std::string tmpGTK="gtk-"+itemToGet;
200
return get_gtk_item(itemToGet,temp,tmpGTK,tmpGTK,defaultItem);
202
std::string get_gtk_item(std::string itemToGet,std::string configItem, std::string defaultItem){
203
std::string temp=sed_i(itemToGet,"-","_");
204
return get_gtk_item(itemToGet,temp,configItem,configItem,defaultItem);
206
std::string get_gtk_item(std::string itemToGetgtk3,std::string itemToGetgtk2,std::string gtk3fileopt,std::string gtk2fileopt, std::string defaultItem){
207
std::string gtkrc2_result, gtkrc3_result,gtk2;
208
std::string GSETTINGS=term_out("which gsettings");
209
std::string GCONF2=term_out("which gconftool-2");
210
if(GSETTINGS.find("gsettings")<GSETTINGS.length()){
211
gtkrc3_result=term_out(GSETTINGS+" get org.gnome.desktop.interface "+itemToGetgtk3);
212
gtkrc3_result=remove_cruft(gtkrc3_result,"\'");
213
gtkrc3_result=remove_cruft(gtkrc3_result,"\'");
214
if(gtkrc3_result.compare("")!=0){return gtkrc3_result;}
216
if(itemToGetgtk2.compare("")!=0){
217
if(GCONF2.find("gconftool-2")<GCONF2.length()){
218
gtk2=term_out(GCONF2+" --get /desktop/gnome/interface/"+itemToGetgtk2);
219
if(gtk2.compare("")!=0){return gtk2;}
222
const char* home = getenv("HOME");
223
if(home==NULL){return defaultItem;}
224
std::string HOME=home;
225
//CHECK/SET GTKRC FILES
226
std::string GTKRC2=HOME + "/.gtkrc-2.0";
227
const char* xdg_config_home=getenv("XDG_CONFIG_HOME");
228
std::string XDG_CONFIG_HOME;
229
if (xdg_config_home!=NULL){
230
XDG_CONFIG_HOME=xdg_config_home;
233
XDG_CONFIG_HOME=HOME +"/.config";
235
if(gtk3fileopt.compare("")!=0){
236
std::string GTKRC3=XDG_CONFIG_HOME + "/gtk-3.0/settings.ini";
237
if(test_file(GTKRC3.c_str())){
238
gtkrc3_result=get_line_with_equal(GTKRC3,gtk3fileopt);
239
gtkrc3_result=remove_cruft(gtkrc3_result,gtk3fileopt);
240
gtkrc3_result=remove_cruft(gtkrc3_result,"\"");
241
gtkrc3_result=remove_cruft(gtkrc3_result,"\"");
242
if(gtkrc3_result.compare("")!=0){return gtkrc3_result;}
245
if(gtk2fileopt.compare("")!=0){
246
if(test_file(GTKRC2.c_str())){
247
gtkrc2_result=get_line_with_equal(GTKRC2,gtk2fileopt);
248
gtkrc2_result=remove_cruft(gtkrc2_result,gtk2fileopt);
249
gtkrc2_result=remove_cruft(gtkrc2_result,"\"");
250
gtkrc2_result=remove_cruft(gtkrc2_result,"\"");
251
if(gtkrc2_result.compare("")!=0){return gtkrc2_result;}
256
std::string get_gtk_themeitem(std::string itemToGet, std::string defaultTheme){
197
257
std::string gtkrc2_result, gtkrc3_result,gtk2;
198
258
if((itemToGet.compare("icon")!=0)&&(itemToGet.compare("")!=0)){
252
312
return defaultTheme;
314
/** this function gets the owner of the filename provided
317
std::string get_file_owner(std::string filename){
320
if(stat(filename.c_str(), &info)==0){
321
struct passwd *pw = getpwuid(info.st_gid);
322
if(pw!=0){result=pw->pw_name;}
326
/** this function gets the group owner of the filename provided
329
std::string get_file_group(std::string filename){
332
if(stat(filename.c_str(), &info)==0){
333
struct group *gr = getgrgid(info.st_gid);
334
if(gr!=0){result=gr->gr_name;}
254
338
/** This function is used for files like *.desktop files to get a value
255
339
* @param filename the filename to look in
256
340
* @param line the line to look for
316
400
/** This function dereferences a symlink to the actual file*/
317
401
std::string get_symlinkpath(std::string symlink){
318
std::vector<char> buf(400);
321
buf.resize(buf.size() + 100);
322
len = ::readlink(symlink.c_str(), &(buf[0]), buf.size());
324
while(buf.size() == len);
327
return (std::string(&(buf[0])));
329
echo_error("Error with symlink");
402
struct stat statinfo;
403
if((lstat(symlink.c_str(), &statinfo)>0)){return symlink;}
404
if ((!S_ISLNK (statinfo.st_mode) && statinfo.st_nlink > 1)
406
(S_ISLNK (statinfo.st_mode))){
407
std::vector<char> buf(400);
410
buf.resize(buf.size() + 100);
411
len = ::readlink(symlink.c_str(), &(buf[0]), buf.size());
413
while(buf.size() == len);
416
return (std::string(&(buf[0])));
419
else{return symlink;}
422
std::string get_user_name(){
428
if(pw!=0){temp=pw->pw_name;}
332
432
/** Return the FIRST match of the 'args' from a file
333
433
* this is like line=`grep -m 1 $args $filename`
822
922
//echo_error("test_file_in_vector_path... found NO files");
925
/** translate ~/ OR $HOME into full user's path to $HOME
926
* @param pathORfilename any path or filename
928
std::string translate_home(std::string pathORfilename){
929
std::string filename=pathORfilename;
930
unsigned int tilde=filename.find("~/");
931
if(tilde<filename.length()){
932
filename=filename.erase(0,tilde+1);
933
filename=home_path()+filename;
935
unsigned int HOMEVAR=filename.find("$HOME");
936
if(HOMEVAR<filename.length()){
937
filename=filename.erase(0,HOMEVAR+1);
938
filename=home_path()+filename;
940
if(filename.compare("")==0)return pathORfilename;
943
std::string process_back_dir_in_filename(std::string filename){
944
unsigned int finder = filename.find("../");
945
std::string pwd=linuxcommon::current_directory();
946
std::string tempPWD=pwd;
947
std::string tempLoop=filename;
949
echo_error("Initial process back dir="+tempLoop);
950
while(finder<tempLoop.length()){
951
tempLoop=tempLoop.substr(finder+counter,std::string::npos);
952
unsigned int slash=tempPWD.rfind("/");
953
if(slash==tempPWD.length()-1){
954
tempPWD=tempPWD.substr(0,slash);
955
slash=tempPWD.rfind("/");
957
tempPWD=tempPWD.substr(0,slash);
958
echo_error("temploop="+tempLoop+"\ntempPWD"+tempPWD);
959
tempLoop=tempPWD+tempLoop;
960
if(tempLoop.compare("")!=0){filename=tempLoop;}
961
finder = tempLoop.find("../");
963
if(filename.find("/")>1){filename="/"+filename;}
964
echo_error("returning:"+filename);
968
/** This will dereference symlinks as well as translate ~/ and $HOME as well as ../
969
* @param filename the filename to process
971
std::string process_filename(std::string filename){
972
echo("Processing="+filename);
973
std::string tmp=process_back_dir_in_filename(filename);
975
std::string tmp2=get_symlinkpath(tmp);
976
if(tmp2.compare("")!=0)tmp=tmp2;
978
std:: string tmp3=translate_home(tmp);
979
if(tmp3.compare("")!=0)tmp=tmp3;
826
984
/** change characters XML doesn't like into ones it does
827
985
* @param input the string to modify
931
1089
///VECTOR FUNCTIONS/////////////////////////////////////////////////
1090
/** get a vector list of all possible current system groups*/
1091
std::vector <std::string> all_groups(){
1092
std::vector <std::string> GROUPvEC;
1093
struct group *groupsPtr = getgrent();
1094
if(groupsPtr != NULL){
1095
for(groupsPtr = getgrent();groupsPtr!=NULL;groupsPtr = getgrent()){
1096
GROUPvEC.push_back(groupsPtr->gr_name);
1099
else{echo_error("No groups found on system!");}
1104
/** get a vector list of all $USER groups */
1105
std::vector <std::string> get_current_user_groups(){
1106
std::vector <std::string> USERgroups;
1107
std::string tmp=get_user_name();
1108
if(tmp.compare("")!=0)return get_user_groups(tmp);
1109
const char* user=getenv("USER");
1111
echo_error("FAILED getting user");
1114
return get_user_groups(user);
1116
/** Get all actual users (UID > 1000) excluding nobody*/
1117
std::vector <std::string> all_users(){
1118
struct passwd *pw = getpwent();
1119
std::vector <std::string> ALLusers;
1121
for(pw = getpwent();pw!=NULL;pw = getpwent()){
1122
std::string TMP=pw->pw_name;
1123
std::string shell=pw->pw_shell;
1124
if( (shell.compare("/usr/sbin/nologin")!=0)&&(shell.compare("/bin/false")!=0) ){
1125
if(TMP.compare("sync")!=0){
1126
ALLusers.push_back(TMP);
1135
/** Get all the users including all processes*/
1136
std::vector <std::string> all_users_including_processes(){
1137
struct passwd *pw = getpwent();
1138
std::vector <std::string> ALLusers;
1140
for(pw = getpwent();pw!=NULL;pw = getpwent()){
1141
std::string TMP=pw->pw_name;
1142
ALLusers.push_back(TMP);
1149
/** get a vector list of all USER's groups
1150
* @param USER the user name to get the groups of
1152
std::vector <std::string> get_user_groups(std::string USER){
1153
std::vector <std::string> USERgroups;
1154
//echo_error("Getting groups for:"+USER)l;
1155
int ngroups=NGROUPS_MAX;
1158
gid_t groups[NGROUPS_MAX];
1159
pw = getpwnam(USER.c_str());
1160
if(getgrouplist(USER.c_str(),pw->pw_gid,groups, &ngroups) == -1){
1161
echo_error("FAILED getting groups for:"+USER);
1164
for (int j = 0; j < ngroups; j++) {
1165
gr = getgrgid(groups[j]);
1166
if (gr != NULL)USERgroups.push_back(gr->gr_name);
932
1170
/** turn a comma delimited string into vector, and join the current items to the vector sent in
933
1171
* @param LINE the comma delimited line
934
1172
* @param Vector the vector to append to (can be empty)
1467
bool file_is_readable(std::string filename){
1468
if(!test_file(filename)){return false;}
1470
std::string file_owner,file_group;
1471
if(stat(filename.c_str(), &info)==0){
1472
struct passwd *pw = getpwuid(info.st_uid);
1473
struct group *gr = getgrgid(info.st_gid);
1474
if(pw!=0){file_owner=pw->pw_name;}
1475
if(gr!=0){file_group=gr->gr_name;}
1476
if(info.st_mode & S_IROTH)return true;
1477
std::string user=get_user_name();
1478
if(file_owner.compare(user)==0){
1479
if(info.st_mode & S_IRUSR)return true;
1481
//TODO get group of user and check group permissions
1485
bool file_is_writable(std::string filename){
1486
if(!test_file(filename)){
1487
filename=get_directory_from_filename(filename);
1490
std::string file_owner,file_group;
1491
if(stat(filename.c_str(), &info)==0){
1492
struct passwd *pw = getpwuid(info.st_uid);
1493
struct group *gr = getgrgid(info.st_gid);
1494
if(pw!=0){file_owner=pw->pw_name;}
1495
if(gr!=0){file_group=gr->gr_name;}
1496
if(info.st_mode & S_IWOTH)return true;
1497
std::string user=get_user_name();
1498
if(file_owner.compare(user)==0){
1499
if(info.st_mode & S_IWUSR)return true;
1501
//TODO get group of user and check group permissions
1223
1505
/** see if a string exists in a vector
1224
1506
* @param vector_to_check the vector to check
1225
1507
* @param item_to_find the item to find
1320
1602
* @param item (icon or gtk)
1321
1603
* @param value the new value
1605
bool switch_gtk_setting(std::string item, std::string value){
1606
std::string GTK2=sed_i(item,"-","_");
1607
std::string tmpGTKconf="gtk-"+item;
1608
std::string gtkrc2_result, gtkrc3_result,gtk2;
1609
if(value.compare("")==0){return false;}
1610
if((item.compare("")==0)){return false;}
1612
std::string GSETTINGS=term_out("which gsettings");
1613
std::string GCONF2=term_out("which gconftool-2");
1614
if(GSETTINGS.find("gsettings")<GSETTINGS.length()){
1615
retval=run_a_program(GSETTINGS+" set org.gnome.desktop.interface "+item+ " \"" +value+"\"");
1616
if(retval!=0){echo_error("Error setting the GSETTINGS theme");}
1618
if(GCONF2.find("gconftool-2")<GCONF2.length()){
1619
retval=run_a_program(GCONF2+" --set --type string /desktop/gnome/interface/"+GTK2+" \""+value+"\"");
1620
if(retval!=0){echo_error("Error setting the GCONF2 theme");}
1622
const char* home = getenv("HOME");
1624
if(home==NULL){fileFix=false;}
1626
std::string HOME=home;
1627
//CHECK/SET GTKRC FILES
1628
std::string GTKRC2=HOME + "/.gtkrc-2.0";
1629
const char* xdg_config_home=getenv("XDG_CONFIG_HOME");
1630
std::string XDG_CONFIG_HOME;
1631
if (xdg_config_home!=NULL){XDG_CONFIG_HOME=xdg_config_home;}
1632
else{XDG_CONFIG_HOME=HOME +"/.config";}
1633
std::string GTKRC3=XDG_CONFIG_HOME + "/gtk-3.0/settings.ini";
1634
if(test_file(GTKRC3.c_str())){
1635
gtkrc3_result=get_line_with_equal(GTKRC3,tmpGTKconf);
1636
gtkrc3_result=remove_cruft(gtkrc3_result,tmpGTKconf);
1637
gtkrc3_result=remove_cruft(gtkrc3_result,"\"");
1638
gtkrc3_result=remove_cruft(gtkrc3_result,"\"");
1639
if(value.compare(gtkrc3_result)!=0){
1641
std::string command="sed -i \"s#"+gtkrc3_result+"#"+value+"#\" "+GTKRC3;
1642
//echo_error(command);
1643
retval=run_a_program(command);
1644
if(retval==0)fileFix=true;
1645
//TODO don't use sed... actually do it :P
1648
if(test_file(GTKRC2.c_str())){
1649
gtkrc2_result=get_line_with_equal(GTKRC2,tmpGTKconf);
1650
gtkrc2_result=remove_cruft(gtkrc2_result,tmpGTKconf);
1651
gtkrc2_result=remove_cruft(gtkrc2_result,"\"");
1652
gtkrc2_result=remove_cruft(gtkrc2_result,"\"");
1653
if(value.compare(gtkrc2_result)!=0){
1657
std::string command="sed -i \"s#"+gtkrc2_result+"#"+value+"#\" "+GTKRC2;
1658
//echo_error(command);
1659
retval=run_a_program(command);
1660
if(retval==0)fileFix=true;
1664
if((retval!=0)&&(!fileFix))return false;
1323
1667
bool switch_gtk_item(std::string item, std::string value){
1324
1668
std::string gtkrc2_result, gtkrc3_result,gtk2;
1325
1669
if(value.compare("")==0){return false;}
1388
1732
if((retval!=0)&&(!fileFix))return false;
1735
/** switch a value out of a file in a Blank=thing setting.
1736
* @param item The ITEM=
1737
* @param value what goes after the '=' (note... this can be blank and still succeed)
1738
* @param filename the file to look in and overwrite
1740
bool switch_equal_line_item(std::string item, std::string value,std::string filename){
1741
if((item.compare("")==0) || (!test_file(filename)) || (!file_is_readable(filename)) || (!file_is_writable(filename)) ){return false;}
1742
std::vector<std::string> fVec = file_to_vector(filename);
1743
std::ifstream inputFileStream(filename.c_str(), std::ifstream::in);
1744
std::string OUT,thisLine;
1745
if(inputFileStream.is_open()){
1746
while (getline(inputFileStream,thisLine)){
1747
std::string LINE=thisLine;
1748
if(thisLine.find(item)<thisLine.length()){
1749
unsigned int found =thisLine.find("=");
1750
if(found < thisLine.length()){
1751
LINE=item+"="+value;
1754
if(OUT.compare("")!=0) OUT=OUT+"\n"+LINE;
1758
if(OUT.compare("")!=0)return save_string_to_file(OUT,filename);
1391
1761
/** test to see if a file exists
1392
1762
* @param fileWithFullPATH the full path and filename to test
1916
unsigned int userUID(std::string user){
1917
unsigned int ERROR=99999;
1918
if(user.compare("")==0)return ERROR;
1920
if(getpwnam(user.c_str())==NULL)return ERROR;
1921
unsigned int UID=pw->pw_uid;
1926
unsigned int userGID(std::string user){
1927
unsigned int ERROR=99999;
1928
if(user.compare("")==0)return ERROR;
1930
if(getpwnam(user.c_str())==NULL)return ERROR;
1931
unsigned int UID=pw->pw_gid;
1936
unsigned int highest_user_UID(){
1937
std::vector <std::string> ALLusers = all_users();
1938
unsigned int TOTAL=0;
1939
for( std::vector<std::string>::iterator itr = ALLusers.begin();
1940
itr!=ALLusers.end();
1942
std::string tmpUser=*itr;
1943
unsigned int tmp=userUID(tmpUser);
1944
if(tmp>TOTAL)TOTAL=tmp;
1549
1948
//INTEGER FUNCTIONS/////////////////////////////////////////////////
1949
int convert_string_to_int(std::string num){
1951
try{NUM=std::stoi(num);}
1952
catch(std::invalid_argument e){return 0;}
1953
catch(std::out_of_range e){return 0;}
1550
1956
/** get a process ID from a name like 'pgrep'
1551
1957
* @param procName the process name to look for