3 Copyright (C) 2011 celeron55, Perttu Ahola <celeron55@gmail.com>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 #include "main.h" // for g_settings
26 #include "serverlist.h"
31 #include <curl/curl.h>
36 std::string getFilePath()
38 std::string serverlist_file = g_settings->get("serverlist_file");
40 std::string rel_path = std::string("client") + DIR_DELIM
41 + "serverlist" + DIR_DELIM
43 std::string path = porting::path_share + DIR_DELIM + rel_path;
47 std::vector<ServerListSpec> getLocal()
49 std::string path = ServerList::getFilePath();
50 std::string liststring;
51 if(fs::PathExists(path))
53 std::ifstream istream(path.c_str(), std::ios::binary);
56 std::ostringstream ostream;
57 ostream << istream.rdbuf();
58 liststring = ostream.str();
63 return ServerList::deSerialize(liststring);
69 static size_t WriteCallback(void *contents, size_t size, size_t nmemb, void *userp)
71 ((std::string*)userp)->append((char*)contents, size * nmemb);
76 std::vector<ServerListSpec> getOnline()
78 std::string liststring;
81 curl = curl_easy_init();
86 curl_easy_setopt(curl, CURLOPT_URL, g_settings->get("serverlist_url").c_str());
87 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, ServerList::WriteCallback);
88 curl_easy_setopt(curl, CURLOPT_WRITEDATA, &liststring);
90 res = curl_easy_perform(curl);
92 errorstream<<"Serverlist at url "<<g_settings->get("serverlist_url")<<" not found (internet connection?)"<<std::endl;
93 curl_easy_cleanup(curl);
96 return ServerList::deSerialize(liststring);
102 Delete a server fromt he local favorites list
104 bool deleteEntry (ServerListSpec server)
106 std::vector<ServerListSpec> serverlist = ServerList::getLocal();
107 for(unsigned i = 0; i < serverlist.size(); i++)
109 if (serverlist[i].address == server.address
110 && serverlist[i].port == server.port)
112 serverlist.erase(serverlist.begin() + i);
116 std::string path = ServerList::getFilePath();
117 std::ofstream stream (path.c_str());
118 if (stream.is_open())
120 stream<<ServerList::serialize(serverlist);
127 Insert a server to the local favorites list
129 bool insert (ServerListSpec server)
132 ServerList::deleteEntry(server);
134 std::vector<ServerListSpec> serverlist = ServerList::getLocal();
136 // Insert new server at the top of the list
137 serverlist.insert(serverlist.begin(), server);
139 std::string path = ServerList::getFilePath();
140 std::ofstream stream (path.c_str());
141 if (stream.is_open())
143 stream<<ServerList::serialize(serverlist);
149 std::vector<ServerListSpec> deSerialize(std::string liststring)
151 std::vector<ServerListSpec> serverlist;
152 std::istringstream stream(liststring);
154 while (std::getline(stream, line))
156 std::transform(line.begin(), line.end(),line.begin(), ::toupper);
157 if (line == "[SERVER]")
159 ServerListSpec thisserver;
160 std::getline(stream, thisserver.name);
161 std::getline(stream, thisserver.address);
162 std::getline(stream, thisserver.port);
163 std::getline(stream, thisserver.description);
164 serverlist.push_back(thisserver);
170 std::string serialize(std::vector<ServerListSpec> serverlist)
172 std::string liststring;
173 for(std::vector<ServerListSpec>::iterator i = serverlist.begin(); i != serverlist.end(); i++)
175 liststring += "[server]\n";
176 liststring += i->name + "\n";
177 liststring += i->address + "\n";
178 liststring += i->port + "\n";
179 liststring += i->description + "\n";
185 } //namespace ServerList