1 /**
2 This file is part of SpringLobby,
3 Copyright (C) 2007-2010
4
5 SpringLobby is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License version 2 as published by
7 the Free Software Foundation.
8
9 springsettings is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with SpringLobby. If not, see <http://www.gnu.org/licenses/>.
16 **/
17
18
19 #include "prdownloader.h"
20
21 #include <lslutils/globalsmanager.h>
22 #include "lib/src/Downloader/IDownloader.h"
23 #include "lib/src/FileSystem/FileSystem.h"
24 #include "lib/src/FileSystem/File.h"
25 #include "../utils/uievents.h"
26 #include "../utils/conversion.h"
27 #include "../utils/globalevents.h"
28 #include "../mainwindow.h"
29 #include "downloadsobserver.h"
30 #include "../utils/debug.h"
31 #include <list>
32
33 #include <wx/log.h>
34 #include <lslunitsync/unitsync.h>
35 #include <lslutils/thread.h>
36 #include <settings.h>
37
GetEngineCat()38 std::string PrDownloader::GetEngineCat()
39 {
40 #ifdef WIN32
41 return "engine_windows";
42 #elif defined(__APPLE__)
43 return "engine_macosx";
44 #elif defined(__x86_64__)
45 return "engine_linux64";
46 #else
47 return "engine_linux";
48 #endif
49 }
50
51 class DownloadItem : public LSL::WorkItem
52 {
53 public:
DownloadItem(std::list<IDownload * > item,IDownloader * loader)54 DownloadItem( std::list<IDownload*> item, IDownloader* loader)
55 : m_item(item)
56 , m_loader(loader)
57 {}
58
Run()59 void Run() {
60 if (!m_item.empty()) {
61 UiEvents::ScopedStatusMessage msg("Downloading: " + m_item.front()->name, 0);
62 //we create this in avance cause m_item gets freed
63 wxString d(_("Download complete: "));
64 d += TowxString(m_item.front()->name);
65 m_loader->download( m_item, sett().GetHTTPMaxParallelDownloads() );
66 std::list<IDownload*>::iterator it;
67 for( it = m_item.begin(); it!=m_item.end(); ++it) {
68 IDownload* dl = *it;
69 switch(dl->cat) {
70 case IDownload::CAT_ENGINE_LINUX:
71 case IDownload::CAT_ENGINE_WINDOWS:
72 case IDownload::CAT_ENGINE_LINUX64:
73 case IDownload::CAT_ENGINE_MACOSX: {
74 fileSystem->extractEngine(dl->name, dl->version);
75 sett().RefreshSpringVersionList(); //FIXME: maybe not thread-save!
76 }
77 default:
78 continue;
79 }
80 }
81 m_loader->freeResult( m_item );
82 UiEvents::ScopedStatusMessage msgcomplete(d, 0);
83 LSL::usync().ReloadUnitSyncLib();
84 GlobalEvent::Send(GlobalEvent::OnUnitsyncReloaded);
85
86 }
87 }
88
89 private:
90 std::list<IDownload*> m_item;
91 IDownloader* m_loader;
92 };
93
94 class SearchItem : public LSL::WorkItem
95 {
96 public:
97 SearchItem(std::list<IDownloader*> loaders, std::string name, IDownload::category cat);
98 void Run();
99
100 private:
101 const std::list<IDownloader*> m_loaders;
102 const std::string m_name;
103 const IDownload::category m_cat;
104 int m_result_size;
105 };
106
SearchItem(std::list<IDownloader * > loaders,const std::string name,IDownload::category cat)107 SearchItem::SearchItem(std::list<IDownloader*> loaders, const std::string name, IDownload::category cat)
108 : m_loaders(loaders)
109 , m_name(name)
110 , m_cat(cat)
111 , m_result_size(0)
112 {}
113
Run()114 void SearchItem::Run()
115 {
116 std::list<IDownload*> results;
117 std::list<IDownloader*>::const_iterator it = m_loaders.begin();
118 for( ; it != m_loaders.end(); ++it ) {
119 (*it)->search(results, m_name, m_cat);
120 if (!results.empty()) {
121 DownloadItem* dl_item = new DownloadItem(results, *it);
122 prDownloader().m_dl_thread->DoWork(dl_item);
123 m_result_size = results.size();
124 return;
125 }
126 }
127 return;
128 }
129
130
PrDownloader()131 PrDownloader::PrDownloader():
132 wxEvtHandler(),
133 m_dl_thread(new LSL::WorkerThread())
134 {
135 IDownloader::Initialize(&downloadsObserver());
136 //UpdateSettings();
137 m_game_loaders.push_back(rapidDownload);
138 m_game_loaders.push_back(httpDownload);
139 m_game_loaders.push_back(plasmaDownload);
140 m_map_loaders.push_back(httpDownload);
141 m_map_loaders.push_back(plasmaDownload);
142 ConnectGlobalEvent(this, GlobalEvent::OnSpringStarted, wxObjectEventFunction(&PrDownloader::OnSpringStarted));
143 ConnectGlobalEvent(this, GlobalEvent::OnSpringTerminated, wxObjectEventFunction(&PrDownloader::OnSpringTerminated));
144 //FIXME: set writepath set in config! can't use unitsyncs data dir, as we want a "global" data dir across all spring versions
145 //fileSystem->setWritePath(path);
146 }
147
~PrDownloader()148 PrDownloader::~PrDownloader()
149 {
150 delete m_dl_thread;
151 IDownloader::Shutdown();
152 }
153
ClearFinished()154 void PrDownloader::ClearFinished()
155 {
156 }
157
UpdateSettings()158 void PrDownloader::UpdateSettings()
159 {
160
161 }
162
RemoveTorrentByName(const std::string &)163 void PrDownloader::RemoveTorrentByName(const std::string &/*name*/)
164 {
165 }
166
GetDownload(const std::string & category,const std::string & name)167 int PrDownloader::GetDownload(const std::string& category, const std::string &name)
168 {
169 if (category == "map") {
170 return Get(m_map_loaders, name, IDownload::CAT_MAPS);
171 } else if (category == "game") {
172 return Get(m_game_loaders, name, IDownload::CAT_GAMES);
173 } else if (category == "engine_linux") {
174 return Get(m_map_loaders, name, IDownload::CAT_ENGINE_LINUX);
175 } else if (category == "engine_linux64") {
176 return Get(m_map_loaders, name, IDownload::CAT_ENGINE_LINUX64);
177 } else if (category == "engine_windows") {
178 return Get(m_map_loaders, name, IDownload::CAT_ENGINE_WINDOWS);
179 } else if (category == "engine_macosx") {
180 return Get(m_map_loaders, name, IDownload::CAT_ENGINE_MACOSX);
181 }
182 wxLogError(_T("Category %s not found"), category.c_str());
183 return -1;
184 }
185
OnSpringStarted(wxCommandEvent &)186 void PrDownloader::OnSpringStarted(wxCommandEvent& /*data*/)
187 {
188 //FIXME: pause downloads
189 }
190
OnSpringTerminated(wxCommandEvent &)191 void PrDownloader::OnSpringTerminated(wxCommandEvent& /*data*/)
192 {
193 //FIXME: resume downloads
194 }
195
Get(std::list<IDownloader * > loaders,const std::string & name,IDownload::category cat)196 int PrDownloader::Get(std::list<IDownloader*> loaders, const std::string &name, IDownload::category cat)
197 {
198 SearchItem* searchItem = new SearchItem(loaders, name, cat);
199 m_dl_thread->DoWork(searchItem);
200 return 1;
201 }
202
prDownloader()203 PrDownloader& prDownloader()
204 {
205 static LSL::Util::LineInfo<PrDownloader> m( AT );
206 static LSL::Util::GlobalObjectHolder<PrDownloader, LSL::Util::LineInfo<PrDownloader> > s_PrDownloader( m );
207 return s_PrDownloader;
208 }
209