commit 336973634e8e77c42c7bdf7f4ff583586d5a09d8
parent 2ca608ac2ca23585d3f03bff9cf5d98d3713293d
Author: cfillion <cfillion@users.noreply.github.com>
Date: Tue, 1 Dec 2015 22:47:44 -0500
implement configurable repository list
Diffstat:
7 files changed, 115 insertions(+), 27 deletions(-)
diff --git a/src/config.cpp b/src/config.cpp
@@ -2,15 +2,87 @@
#include "path.hpp"
+#include <swell/swell.h>
+
+#include <reaper_plugin_functions.h>
+
+using namespace std;
+
+static const char *REPOS_GROUP = "repositories";
+
+static string RepoNameKey(const int i)
+{
+ static const string key = "name";
+ return key + to_string(i);
+}
+
+static string RepoUrlKey(const int i)
+{
+ static const string key = "url";
+ return key + to_string(i);
+}
+
+static const int URL_SIZE = 2083;
+
Config::Config()
{
}
+void Config::fillDefaults()
+{
+ m_repositories.push_back({"ReaScripts",
+ "https://github.com/ReaTeam/ReaScripts/raw/master/index.xml"});
+}
+
void Config::read(const Path &path)
{
- m_path = path.cjoin();
+ m_path = path.join();
+
+ if(!file_exists(m_path.c_str())) {
+ fillDefaults();
+ write();
+ return;
+ }
+
+ readRepositories();
}
void Config::write() const
{
+ writeRepositories();
+}
+
+void Config::readRepositories()
+{
+ int i = 0;
+
+ do {
+ char name[URL_SIZE];
+ GetPrivateProfileString(REPOS_GROUP, RepoNameKey(i).c_str(),
+ "", name, sizeof(name), m_path.c_str());
+
+ char url[URL_SIZE];
+ GetPrivateProfileString(REPOS_GROUP, RepoUrlKey(i).c_str(),
+ "", url, sizeof(url), m_path.c_str());
+
+ if(!strlen(name) || !strlen(url))
+ break;
+
+ m_repositories.push_back({name, url});
+ } while(++i);
+}
+
+void Config::writeRepositories() const
+{
+ const int size = m_repositories.size();
+
+ for(int i = 0; i < size; i++) {
+ const Repository &repo = m_repositories[i];
+
+ WritePrivateProfileString(REPOS_GROUP, RepoNameKey(i).c_str(),
+ repo.name().c_str(), m_path.c_str());
+
+ WritePrivateProfileString(REPOS_GROUP, RepoUrlKey(i).c_str(),
+ repo.url().c_str(), m_path.c_str());
+ }
}
diff --git a/src/config.hpp b/src/config.hpp
@@ -4,17 +4,18 @@
#include <string>
#include <vector>
-struct Repository {
- Repository(const std::string &name, const char *url)
+class Repository {
+public:
+ Repository(const std::string &name, const std::string &url)
: m_name(name), m_url(url)
{}
const std::string &name() const { return m_name; }
- const char *url() const { return m_url; }
+ const std::string &url() const { return m_url; }
private:
- const std::string &m_name;
- const char *m_url;
+ std::string m_name;
+ std::string m_url;
};
typedef std::vector<Repository> RepositoryList;
@@ -31,8 +32,12 @@ public:
const RepositoryList &repositories() const { return m_repositories; }
private:
- const char *m_path;
+ void fillDefaults();
+
+ std::string m_path;
+ void readRepositories();
+ void writeRepositories() const;
RepositoryList m_repositories;
};
diff --git a/src/download.cpp b/src/download.cpp
@@ -10,11 +10,9 @@ vector<Download *> Download::s_active;
static const int DOWNLOAD_TIMEOUT = 5;
-Download::Download(const char *url)
- : m_threadHandle(0)
+Download::Download(const std::string &url)
+ : m_threadHandle(0), m_url(url)
{
- m_url = url;
-
reset();
}
@@ -102,7 +100,7 @@ DWORD WINAPI Download::Worker(void *ptr)
string contents;
CURL *curl = curl_easy_init();
- curl_easy_setopt(curl, CURLOPT_URL, download->url());
+ curl_easy_setopt(curl, CURLOPT_URL, download->url().c_str());
curl_easy_setopt(curl, CURLOPT_USERAGENT, "ReaPack/1.0 (REAPER)");
curl_easy_setopt(curl, CURLOPT_TIMEOUT, DOWNLOAD_TIMEOUT);
@@ -217,7 +215,7 @@ DownloadQueue::~DownloadQueue()
}
}
-void DownloadQueue::push(const char *url, DownloadCallback cb)
+void DownloadQueue::push(const std::string &url, DownloadCallback cb)
{
Download *download = new Download(url);
download->addCallback(cb);
diff --git a/src/download.hpp b/src/download.hpp
@@ -21,10 +21,10 @@ public:
WriteError,
};
- Download(const char *url);
+ Download(const std::string &url);
~Download();
- const char *url() const { return m_url; }
+ const std::string &url() const { return m_url; }
bool isFinished();
bool isAborted();
@@ -51,7 +51,7 @@ private:
int m_status;
std::string m_contents;
- const char *m_url;
+ std::string m_url;
std::vector<DownloadCallback> m_callback;
WDL_Mutex m_mutex;
@@ -61,7 +61,7 @@ class DownloadQueue {
public:
~DownloadQueue();
- void push(const char *, DownloadCallback);
+ void push(const std::string &url, DownloadCallback);
private:
std::queue<Download *> m_queue;
diff --git a/src/main.cpp b/src/main.cpp
@@ -13,7 +13,12 @@ bool commandHook(const int id, const int flag)
extern "C" REAPER_PLUGIN_DLL_EXPORT int REAPER_PLUGIN_ENTRYPOINT(
REAPER_PLUGIN_HINSTANCE instance, reaper_plugin_info_t *rec)
{
- if(!rec || rec->caller_version != REAPER_PLUGIN_VERSION || !rec->GetFunc)
+ if(!rec) {
+ reapack.cleanup();
+ return 0;
+ }
+
+ if(rec->caller_version != REAPER_PLUGIN_VERSION || !rec->GetFunc)
return 0;
if(REAPERAPI_LoadAPI(rec->GetFunc) > 0)
diff --git a/src/reapack.cpp b/src/reapack.cpp
@@ -4,7 +4,7 @@
#include <fstream>
-#include "reaper_plugin_functions.h"
+#include <reaper_plugin_functions.h>
using namespace std;
@@ -22,6 +22,11 @@ void ReaPack::init(REAPER_PLUGIN_HINSTANCE instance, reaper_plugin_info_t *rec)
RecursiveCreateDirectory(m_dbPath.cjoin(), 0);
}
+void ReaPack::cleanup()
+{
+ m_config.write();
+}
+
void ReaPack::setupAction(const char *name, const char *desc,
gaccel_register_t *action, ActionCallback callback)
{
@@ -63,9 +68,11 @@ void ReaPack::synchronizeAll()
void ReaPack::synchronize(const Repository &repo)
{
- m_downloadQueue.push(repo.url(), [=](const int status, const string &contents) {
- if(status != 200)
+ m_downloadQueue.push(repo.url(), [=](const int code, const string &data) {
+ if(code != 200) {
+ ShowMessageBox(data.c_str(), repo.name().c_str(), 0);
return;
+ }
const Path path = m_dbPath + (repo.name() + ".xml");
@@ -75,7 +82,7 @@ void ReaPack::synchronize(const Repository &repo)
return;
}
- file << contents;
+ file << data;
file.close();
synchronize(Database::load(path.cjoin()));
@@ -105,9 +112,9 @@ void ReaPack::installPackage(PackagePtr pkg)
{
const char *url = pkg->lastVersion()->source(0)->url().c_str();
- m_downloadQueue.push(url, [=](const int status, const string &contents) {
- if(status != 200) {
- ShowMessageBox(contents.c_str(), "Download failure (debug)", 0);
+ m_downloadQueue.push(url, [=](const int code, const string &data) {
+ if(code != 200) {
+ ShowMessageBox(data.c_str(), pkg->name().c_str(), 0);
return;
}
@@ -120,7 +127,7 @@ void ReaPack::installPackage(PackagePtr pkg)
return;
}
- file << contents;
+ file << data;
file.close();
});
}
diff --git a/src/reapack.hpp b/src/reapack.hpp
@@ -8,7 +8,7 @@
#include "database.hpp"
#include "download.hpp"
-#include "reaper_plugin.h"
+#include <reaper_plugin.h>
typedef std::function<void()> ActionCallback;
@@ -17,6 +17,7 @@ public:
gaccel_register_t action;
void init(REAPER_PLUGIN_HINSTANCE, reaper_plugin_info_t *);
+ void cleanup();
void setupAction(const char *name, const char *desc,
gaccel_register_t *action, ActionCallback callback);