/* This file is part of dc_qt. dc_qt 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 2 of the License, or (at your option) any later version. dc_qt 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 dc_qt; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ #include "dc_settings.h" #include "dc_parse.h" #include "util.h" //#include "debugdlg.h" // DO NOT use debugWin->anything() in this file plz #include #include #include #include #include #include #include using namespace std; typedef pair Pair; typedef pair StrPair; #define DEFAULT_HUBLIST "http://www.hublist.org/PublicHubList.xml.bz2" #define COMPATIBILITY_010_MODE // define SETTINGS_DEBUG dc_settings::dc_settings() { hubLists+=DEFAULT_HUBLIST; } #ifdef COMPATIBILITY_010_MODE void cleanup_settings(const dc_settings::KeyMap &scommon, dc_settings::KeyMap &sdefault) { dc_settings::KeyMap::const_iterator ci = scommon.begin(); while (ci != scommon.end()) { dc_settings::KeyMap::iterator di = sdefault.find(ci.key()); if (di != sdefault.end()) { sdefault.erase(di); } ci++; } } #endif int dc_settings::init() { user_home_dir = QDir::homeDirPath(); // getenv("HOME"); settings_file = user_home_dir + "/.dc_qt/dc_qt.conf"; favourites_file = user_home_dir + "/.dc_qt/dc_qt.favourites"; set_profile("default"); set_setting("port","1412"); set_setting("nick","dcqtuser"); set_setting("description","dcqt user"); set_setting("email","dcqtuser"); set_setting("connection","Cable"); set_setting("slots","3"); set_setting("local_ip",""); set_setting("common","font_encoding","CP1252"); set_setting("allow_file_list_ul","true"); set_setting("common","dl_rate_limit","0"); set_setting("desc_tag","dctc"); set_setting("same_hub_only","false"); set_setting("passive_mode","false"); set_setting("ul_rate_limit","0"); set_setting("common","dl_dir","/tmp"); set_setting("common","font","Arial,12,-1,5,50,0,0,0,0,0"); set_setting("use_proxy","false"); set_setting("use_socks","false"); set_setting("common","tool_iconset","Default"); set_setting("common","nick_highlight","true"); set_setting("common","chat_timestamp","true"); QDir dc_dir( user_home_dir + "/.dc_qt" ); if( !dc_dir.exists() ) { dc_dir.mkdir( user_home_dir + "/.dc_qt" ); return NO_SETTINGS; } if(!load_from_file()) return ERROR_LOADING_PREFS; if(!load_favourites_file()) return ERROR_LOADING_FAVOURITES; return SUCCESS; } void dc_settings::set_profile(const QString &name) { current_profile = name; } void dc_settings::remove_profile(const QString &name) { cerr << "remove profile: " << name << "\n"; settings.erase(name); } QStringList dc_settings::get_profiles() { QStringList ret = settings.keys(); ret.remove("common"); return ret; } QString dc_settings::get_setting(const QString& key) { return get_setting(current_profile,key); } QString dc_settings::get_setting(const QString& profile, const QString& key) { if(settings.find(profile)!=settings.end()) { if(settings[profile].find(key)!=settings[profile].end()) { return settings[profile][key]; } } // Setting not found - check common if(settings.find("common")!=settings.end()) { if(settings["common"].find(key)!=settings["common"].end()) { return settings["common"][key]; } } return QString(""); } void dc_settings::set_setting(const QString& key, const QString &value) { set_setting(current_profile,key,value); } void dc_settings::set_setting(const QString& profile,const QString& key, const QString &value) { //cerr << "set_setting: " << profile << "," << key << "," << "value\n"; settings[profile][key] = value; } bool dc_settings::load_from_file() { // Default values goes here in case they're not in the file. QFile file(settings_file); if (file.open(IO_ReadOnly)) { QTextStream stream(&file); QString inbuf; QString profile; while (!stream.eof()) { inbuf = stream.readLine(); if(inbuf.startsWith("\\profile")) profile = inbuf.section(" ",1,1); else { if(profile.isEmpty()) continue; int pos = inbuf.find("="); if(pos <= 0) continue; set_setting(profile,inbuf.left(pos),inbuf.right(inbuf.length()-pos-1)); } } } else return false; return true; } // this is currently broken. A hub that has an '=' in it will not be // read in correctly. bool dc_settings::load_favourites_file() { QFile file(favourites_file); if( file.open(IO_ReadOnly) ) { QTextStream stream(&file); QString inbuf; QString name,ip,profile; bool autoc; QString pwd = QString::null; bool inserted = true; while( !stream.eof() ) { inbuf = stream.readLine(); if( inbuf[0] == '#' ) continue; dc_parse p(inbuf,'='); if( p.get_str(0) == "name" ) { if(!inserted) favourites.insert(pair(name,FavData(ip,pwd,autoc,profile))); name = p.get_str(1); } else if( p.get_str(0) == "address" ) { ip = p.get_str(1); } else if( p.get_str(0) == "password") { pwd = p.get_str(1); if(pwd=="") pwd = QString::null; inserted = false; } else if( p.get_str(0) == "autoconnect") { autoc = p.get_str(1) == "true"; } else if( p.get_str(0) == "profile") { profile = p.get_str(1); } else if( p.get_str(0) == "hublisturl") { if(p.get_str(1)!=DEFAULT_HUBLIST) hubLists+=p.get_str(1); } } if(!inserted) favourites.insert(pair(name,FavData(ip,pwd,autoc,profile))); //if(!hubLists.contains(DEFAULT_HUBLIST)) // hubLists+=DEFAULT_HUBLIST; return true; } //if(!hubLists.contains(DEFAULT_HUBLIST)) // hubLists+=DEFAULT_HUBLIST; return false; } bool dc_settings::save_to_file() { #ifdef COMPATIBILITY_010_MODE cleanup_settings(settings["common"], settings["default"]); #endif QFile file(settings_file); if(file.open(IO_WriteOnly)) { QTextStream stream(&file); for(ProfileMap::iterator ik = settings.begin(); ik != settings.end(); ++ik) { stream << "\\profile " << ik.key() << "\n"; for(KeyMap::iterator iv = ik.data().begin(); iv != ik.data().end(); ++iv) { stream << iv.key() << "=" << iv.data() << "\n"; //if(iv.data().isEmpty()) //cerr << ik.key() << ":" << iv.key() << "=" << "\n"; //else // cerr << ik.key() << ":" << iv.key() << "=" << iv.data() << "\n"; } } file.close(); return true; } return false; } void dc_settings::add_favourite(const QString &name,const QString &address,const QString &password,bool autoc,const QString& prof) { favourites.erase(name); favourites.insert( pair( name, FavData(address,password,autoc,prof) ) ); } void dc_settings::del_favourite(const QString &name) { favourites.erase(name); } bool dc_settings::get_favourite(int i,QString &name, QString &address, QString &password,bool &autoc,QString &prof) { StrMap::iterator it = favourites.begin(); while(i--) it++; if(it==favourites.end()) return false; name = (*it).first; address = (*it).second.ip; password = (*it).second.pwd; autoc = (*it).second.autoconnect; prof = (*it).second.profile; return true; } const hub_info dc_settings::get_favourite(const QString &name) { StrMap::iterator it = favourites.find(name); if( it == favourites.end() ) return hub_info("", "", "", ""); else return hub_info(name, (*it).second.ip, "", "", (*it).second.pwd); } bool dc_settings::save_favourites_file() { QFile file(favourites_file); if( file.open(IO_WriteOnly) ) { QTextStream stream(&file); QString name,ip,pwd,profile; bool autoc; stream << "# dc_qt favourite file. Generated automatically, DO NOT EDIT.\n"; for(int i=0;i < num_favourites();i++) { get_favourite(i,name,ip,pwd,autoc,profile); if(pwd.isEmpty()) pwd = ""; stream << "name=" << name << "\naddress=" << ip << "\npassword=" << pwd << "\n"; if(autoc) stream << "autoconnect=true\n"; else stream << "autoconnect=false\n"; stream << "profile=" << profile << "\n"; } for(unsigned int i=0;i