summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRemko Tronçon <git@el-tramo.be>2010-10-09 16:32:50 (GMT)
committerRemko Tronçon <git@el-tramo.be>2010-10-09 22:33:20 (GMT)
commitdc951ebf7e4ab6fce39f16687be1e93f8140b71f (patch)
tree4eee56d72b8773049522fbaff8d9fb1ee0a5c978 /SwifTools/Application
parent64ab77e910579bce3fe99b107a82753a7f32d77c (diff)
downloadswift-contrib-dc951ebf7e4ab6fce39f16687be1e93f8140b71f.zip
swift-contrib-dc951ebf7e4ab6fce39f16687be1e93f8140b71f.tar.bz2
Moved Application from Swiften into SwifTools.
Diffstat (limited to 'SwifTools/Application')
-rw-r--r--SwifTools/Application/ApplicationPathProvider.cpp47
-rw-r--r--SwifTools/Application/ApplicationPathProvider.h36
-rw-r--r--SwifTools/Application/CocoaApplication.h19
-rw-r--r--SwifTools/Application/CocoaApplication.mm24
-rw-r--r--SwifTools/Application/MacOSXApplicationPathProvider.cpp49
-rw-r--r--SwifTools/Application/MacOSXApplicationPathProvider.h27
-rw-r--r--SwifTools/Application/PlatformApplicationPathProvider.h26
-rw-r--r--SwifTools/Application/SConscript26
-rw-r--r--SwifTools/Application/UnitTest/ApplicationPathProviderTest.cpp49
-rw-r--r--SwifTools/Application/UnixApplicationPathProvider.cpp66
-rw-r--r--SwifTools/Application/UnixApplicationPathProvider.h34
-rw-r--r--SwifTools/Application/WindowsApplicationPathProvider.cpp27
-rw-r--r--SwifTools/Application/WindowsApplicationPathProvider.h38
13 files changed, 468 insertions, 0 deletions
diff --git a/SwifTools/Application/ApplicationPathProvider.cpp b/SwifTools/Application/ApplicationPathProvider.cpp
new file mode 100644
index 0000000..bf0a19e
--- /dev/null
+++ b/SwifTools/Application/ApplicationPathProvider.cpp
@@ -0,0 +1,47 @@
+/*
+ * Copyright (c) 2010 Remko Tronçon
+ * Licensed under the GNU General Public License v3.
+ * See Documentation/Licenses/GPLv3.txt for more information.
+ */
+
+#include <boost/filesystem.hpp>
+#include <iostream>
+
+#include "SwifTools/Application/ApplicationPathProvider.h"
+#include "Swiften/Base/foreach.h"
+
+namespace Swift {
+
+ApplicationPathProvider::ApplicationPathProvider(const String& applicationName) : applicationName(applicationName) {
+}
+
+ApplicationPathProvider::~ApplicationPathProvider() {
+}
+
+boost::filesystem::path ApplicationPathProvider::getAvatarDir() const {
+ return getDataDir() / "avatars";
+}
+
+boost::filesystem::path ApplicationPathProvider::getProfileDir(const String& profile) const {
+ boost::filesystem::path result(getHomeDir() / profile.getUTF8String());
+ try {
+ boost::filesystem::create_directory(result);
+ }
+ catch (const boost::filesystem::filesystem_error& e) {
+ std::cerr << "ERROR: " << e.what() << std::endl;
+ }
+ return result;
+}
+
+boost::filesystem::path ApplicationPathProvider::getResourcePath(const String& resource) const {
+ std::vector<boost::filesystem::path> resourcePaths = getResourceDirs();
+ foreach(const boost::filesystem::path& resourcePath, resourcePaths) {
+ boost::filesystem::path r(resourcePath / resource.getUTF8String());
+ if (boost::filesystem::exists(r)) {
+ return r;
+ }
+ }
+ return boost::filesystem::path();
+}
+
+}
diff --git a/SwifTools/Application/ApplicationPathProvider.h b/SwifTools/Application/ApplicationPathProvider.h
new file mode 100644
index 0000000..7bd2630
--- /dev/null
+++ b/SwifTools/Application/ApplicationPathProvider.h
@@ -0,0 +1,36 @@
+/*
+ * Copyright (c) 2010 Remko Tronçon
+ * Licensed under the GNU General Public License v3.
+ * See Documentation/Licenses/GPLv3.txt for more information.
+ */
+
+#pragma once
+
+#include <boost/filesystem.hpp>
+#include <vector>
+
+#include "Swiften/Base/String.h"
+
+namespace Swift {
+ class ApplicationPathProvider {
+ public:
+ ApplicationPathProvider(const String& applicationName);
+ virtual ~ApplicationPathProvider();
+
+ boost::filesystem::path getAvatarDir() const;
+ virtual boost::filesystem::path getHomeDir() const = 0;
+ virtual boost::filesystem::path getDataDir() const = 0;
+ virtual boost::filesystem::path getExecutableDir() const = 0;
+ boost::filesystem::path getProfileDir(const String& profile) const;
+ boost::filesystem::path getResourcePath(const String& resource) const;
+
+ protected:
+ virtual std::vector<boost::filesystem::path> getResourceDirs() const = 0;
+ const String& getApplicationName() const {
+ return applicationName;
+ }
+
+ private:
+ String applicationName;
+ };
+}
diff --git a/SwifTools/Application/CocoaApplication.h b/SwifTools/Application/CocoaApplication.h
new file mode 100644
index 0000000..3b19d28
--- /dev/null
+++ b/SwifTools/Application/CocoaApplication.h
@@ -0,0 +1,19 @@
+/*
+ * Copyright (c) 2010 Remko Tronçon
+ * Licensed under the GNU General Public License v3.
+ * See Documentation/Licenses/GPLv3.txt for more information.
+ */
+
+#pragma once
+
+namespace Swift {
+ class CocoaApplication {
+ public:
+ CocoaApplication();
+ ~CocoaApplication();
+
+ private:
+ class Private;
+ Private* d;
+ };
+}
diff --git a/SwifTools/Application/CocoaApplication.mm b/SwifTools/Application/CocoaApplication.mm
new file mode 100644
index 0000000..edfdc25
--- /dev/null
+++ b/SwifTools/Application/CocoaApplication.mm
@@ -0,0 +1,24 @@
+#include "SwifTools/Application/CocoaApplication.h"
+
+#include <AppKit/AppKit.h>
+#include <Cocoa/Cocoa.h>
+
+namespace Swift {
+
+class CocoaApplication::Private {
+ public:
+ NSAutoreleasePool* autoReleasePool_;
+};
+
+CocoaApplication::CocoaApplication() {
+ d = new CocoaApplication::Private();
+ NSApplicationLoad();
+ d->autoReleasePool_ = [[NSAutoreleasePool alloc] init];
+}
+
+CocoaApplication::~CocoaApplication() {
+ [d->autoReleasePool_ release];
+ delete d;
+}
+
+}
diff --git a/SwifTools/Application/MacOSXApplicationPathProvider.cpp b/SwifTools/Application/MacOSXApplicationPathProvider.cpp
new file mode 100644
index 0000000..eb6c63f
--- /dev/null
+++ b/SwifTools/Application/MacOSXApplicationPathProvider.cpp
@@ -0,0 +1,49 @@
+/*
+ * Copyright (c) 2010 Remko Tronçon
+ * Licensed under the GNU General Public License v3.
+ * See Documentation/Licenses/GPLv3.txt for more information.
+ */
+
+#include "SwifTools/Application/MacOSXApplicationPathProvider.h"
+
+#include <iostream>
+#include <mach-o/dyld.h>
+
+#include "Swiften/Base/ByteArray.h"
+
+namespace Swift {
+
+MacOSXApplicationPathProvider::MacOSXApplicationPathProvider(const String& name) : ApplicationPathProvider(name) {
+ resourceDirs.push_back(getExecutableDir() / "../Resources");
+ resourceDirs.push_back(getExecutableDir() / "../resources"); // Development
+}
+
+boost::filesystem::path MacOSXApplicationPathProvider::getDataDir() const {
+ boost::filesystem::path result(getHomeDir() / "Library/Application Support" / getApplicationName().getUTF8String());
+ try {
+ boost::filesystem::create_directory(result);
+ }
+ catch (const boost::filesystem::filesystem_error& e) {
+ std::cerr << "ERROR: " << e.what() << std::endl;
+ }
+ return result;
+}
+
+boost::filesystem::path MacOSXApplicationPathProvider::getHomeDir() const {
+ return boost::filesystem::path(getenv("HOME"));
+}
+
+
+boost::filesystem::path MacOSXApplicationPathProvider::getExecutableDir() const {
+ ByteArray path;
+ uint32_t size = 4096;
+ path.resize(size);
+ if (_NSGetExecutablePath(path.getData(), &size) == 0) {
+ return boost::filesystem::path(path.toString().getUTF8Data()).parent_path();
+ }
+ else {
+ return boost::filesystem::path();
+ }
+}
+
+}
diff --git a/SwifTools/Application/MacOSXApplicationPathProvider.h b/SwifTools/Application/MacOSXApplicationPathProvider.h
new file mode 100644
index 0000000..a914ded
--- /dev/null
+++ b/SwifTools/Application/MacOSXApplicationPathProvider.h
@@ -0,0 +1,27 @@
+/*
+ * Copyright (c) 2010 Remko Tronçon
+ * Licensed under the GNU General Public License v3.
+ * See Documentation/Licenses/GPLv3.txt for more information.
+ */
+
+#pragma once
+
+#include "SwifTools/Application/ApplicationPathProvider.h"
+
+namespace Swift {
+ class MacOSXApplicationPathProvider : public ApplicationPathProvider {
+ public:
+ MacOSXApplicationPathProvider(const String& name);
+
+ virtual boost::filesystem::path getHomeDir() const;
+ boost::filesystem::path getDataDir() const;
+ virtual boost::filesystem::path getExecutableDir() const;
+
+ virtual std::vector<boost::filesystem::path> getResourceDirs() const {
+ return resourceDirs;
+ }
+
+ private:
+ std::vector<boost::filesystem::path> resourceDirs;
+ };
+}
diff --git a/SwifTools/Application/PlatformApplicationPathProvider.h b/SwifTools/Application/PlatformApplicationPathProvider.h
new file mode 100644
index 0000000..bb9bfa9
--- /dev/null
+++ b/SwifTools/Application/PlatformApplicationPathProvider.h
@@ -0,0 +1,26 @@
+/*
+ * Copyright (c) 2010 Remko Tronçon
+ * Licensed under the GNU General Public License v3.
+ * See Documentation/Licenses/GPLv3.txt for more information.
+ */
+
+#pragma once
+
+#include "Swiften/Base/Platform.h"
+
+#if defined(SWIFTEN_PLATFORM_MACOSX)
+#include "SwifTools/Application/MacOSXApplicationPathProvider.h"
+namespace Swift {
+ typedef MacOSXApplicationPathProvider PlatformApplicationPathProvider;
+}
+#elif defined(SWIFTEN_PLATFORM_WIN32)
+#include "SwifTools/Application/WindowsApplicationPathProvider.h"
+namespace Swift {
+ typedef WindowsApplicationPathProvider PlatformApplicationPathProvider;
+}
+#else
+#include "SwifTools/Application/UnixApplicationPathProvider.h"
+namespace Swift {
+ typedef UnixApplicationPathProvider PlatformApplicationPathProvider;
+}
+#endif
diff --git a/SwifTools/Application/SConscript b/SwifTools/Application/SConscript
new file mode 100644
index 0000000..0097bca
--- /dev/null
+++ b/SwifTools/Application/SConscript
@@ -0,0 +1,26 @@
+Import("swiftools_env", "env")
+
+sources = [
+ "ApplicationPathProvider.cpp",
+ ]
+
+if swiftools_env["PLATFORM"] == "darwin" and swiftools_env["target"] == "native" :
+ sources += [
+ "CocoaApplication.mm",
+ "MacOSXApplicationPathProvider.cpp",
+ ]
+elif swiftools_env["PLATFORM"] == "win32" :
+ sources += [
+ "WindowsApplicationPathProvider.cpp"
+ ]
+else :
+ sources += [
+ "UnixApplicationPathProvider.cpp"
+ ]
+
+objects = swiftools_env.StaticObject(sources)
+swiftools_env.Append(SWIFTOOLS_OBJECTS = [objects])
+
+env.Append(UNITTEST_SOURCES = [
+ File("UnitTest/ApplicationPathProviderTest.cpp")
+ ])
diff --git a/SwifTools/Application/UnitTest/ApplicationPathProviderTest.cpp b/SwifTools/Application/UnitTest/ApplicationPathProviderTest.cpp
new file mode 100644
index 0000000..cd171cb
--- /dev/null
+++ b/SwifTools/Application/UnitTest/ApplicationPathProviderTest.cpp
@@ -0,0 +1,49 @@
+/*
+ * Copyright (c) 2010 Remko Tronçon
+ * Licensed under the GNU General Public License v3.
+ * See Documentation/Licenses/GPLv3.txt for more information.
+ */
+
+#include <cppunit/extensions/HelperMacros.h>
+#include <cppunit/extensions/TestFactoryRegistry.h>
+
+#include "SwifTools/Application/PlatformApplicationPathProvider.h"
+#include "Swiften/Base/String.h"
+
+using namespace Swift;
+
+class ApplicationPathProviderTest : public CppUnit::TestFixture {
+ CPPUNIT_TEST_SUITE(ApplicationPathProviderTest);
+ CPPUNIT_TEST(testGetDataDir);
+ CPPUNIT_TEST(testGetExecutableDir);
+ CPPUNIT_TEST_SUITE_END();
+
+ public:
+ void setUp() {
+ testling_ = new PlatformApplicationPathProvider("SwiftTest");
+ }
+
+ void tearDown() {
+ delete testling_;
+ }
+
+ void testGetDataDir() {
+ boost::filesystem::path dir = testling_->getDataDir();
+
+ CPPUNIT_ASSERT(boost::filesystem::exists(dir));
+ CPPUNIT_ASSERT(boost::filesystem::is_directory(dir));
+
+ boost::filesystem::remove(dir);
+ }
+
+ void testGetExecutableDir() {
+ boost::filesystem::path dir = testling_->getExecutableDir();
+ CPPUNIT_ASSERT(boost::filesystem::is_directory(dir));
+ CPPUNIT_ASSERT(String(dir.string()).endsWith("UnitTest"));
+ }
+
+ private:
+ ApplicationPathProvider* testling_;
+};
+
+CPPUNIT_TEST_SUITE_REGISTRATION(ApplicationPathProviderTest);
diff --git a/SwifTools/Application/UnixApplicationPathProvider.cpp b/SwifTools/Application/UnixApplicationPathProvider.cpp
new file mode 100644
index 0000000..67252ec
--- /dev/null
+++ b/SwifTools/Application/UnixApplicationPathProvider.cpp
@@ -0,0 +1,66 @@
+/*
+ * Copyright (c) 2010 Remko Tronçon
+ * Licensed under the GNU General Public License v3.
+ * See Documentation/Licenses/GPLv3.txt for more information.
+ */
+
+#include "SwifTools/Application/UnixApplicationPathProvider.h"
+
+#include <boost/filesystem/convenience.hpp>
+
+namespace Swift {
+
+UnixApplicationPathProvider::UnixApplicationPathProvider(const String& name) : ApplicationPathProvider(name) {
+ resourceDirs.push_back(getExecutableDir() / "../resources"); // Development
+ char* xdgDataDirs = getenv("XDG_DATA_DIRS");
+ if (xdgDataDirs) {
+ std::vector<String> dataDirs = String(xdgDataDirs).split(':');
+ if (!dataDirs.empty()) {
+ foreach(const String& dir, dataDirs) {
+ resourceDirs.push_back(boost::filesystem::path(dir.getUTF8String()) / "swift");
+ }
+ return;
+ }
+ }
+ resourceDirs.push_back("/usr/local/share/swift");
+ resourceDirs.push_back("/usr/share/swift");
+}
+
+boost::filesystem::path UnixApplicationPathProvider::getHomeDir() const {
+ return boost::filesystem::path(getenv("HOME"));
+}
+
+boost::filesystem::path UnixApplicationPathProvider::getDataDir() const {
+ char* xdgDataHome = getenv("XDG_DATA_HOME");
+ String dataDir;
+ if (xdgDataHome) {
+ dataDir = String(xdgDataHome);
+ }
+
+ boost::filesystem::path dataPath = (dataDir.isEmpty() ?
+ getHomeDir() / ".local" / "share"
+ : boost::filesystem::path(dataDir.getUTF8String())) / getApplicationName().getLowerCase().getUTF8String();
+
+ try {
+ boost::filesystem::create_directories(dataPath);
+ }
+ catch (const boost::filesystem::filesystem_error& e) {
+ std::cerr << "ERROR: " << e.what() << std::endl;
+ }
+ return dataPath;
+}
+
+boost::filesystem::path UnixApplicationPathProvider::getExecutableDir() const {
+ ByteArray path;
+ path.resize(4096);
+ size_t size = readlink("/proc/self/exe", path.getData(), path.getSize());
+ if (size > 0) {
+ path.resize(size);
+ return boost::filesystem::path(path.toString().getUTF8Data()).parent_path();
+ }
+ else {
+ return boost::filesystem::path();
+ }
+}
+
+}
diff --git a/SwifTools/Application/UnixApplicationPathProvider.h b/SwifTools/Application/UnixApplicationPathProvider.h
new file mode 100644
index 0000000..2ef04ea
--- /dev/null
+++ b/SwifTools/Application/UnixApplicationPathProvider.h
@@ -0,0 +1,34 @@
+/*
+ * Copyright (c) 2010 Remko Tronçon
+ * Licensed under the GNU General Public License v3.
+ * See Documentation/Licenses/GPLv3.txt for more information.
+ */
+
+#pragma once
+
+#include "SwifTools/Application/ApplicationPathProvider.h"
+
+#include <iostream>
+#include <unistd.h>
+
+#include "Swiften/Base/ByteArray.h"
+#include "Swiften/Base/foreach.h"
+
+namespace Swift {
+ class UnixApplicationPathProvider : public ApplicationPathProvider {
+ public:
+ UnixApplicationPathProvider(const String& name);
+
+ virtual boost::filesystem::path getHomeDir() const;
+ boost::filesystem::path getDataDir() const;
+ virtual boost::filesystem::path getExecutableDir() const;
+
+ virtual std::vector<boost::filesystem::path> getResourceDirs() const {
+ return resourceDirs;
+ }
+
+ private:
+ std::vector<boost::filesystem::path> resourceDirs;
+ };
+}
+
diff --git a/SwifTools/Application/WindowsApplicationPathProvider.cpp b/SwifTools/Application/WindowsApplicationPathProvider.cpp
new file mode 100644
index 0000000..ed692d7
--- /dev/null
+++ b/SwifTools/Application/WindowsApplicationPathProvider.cpp
@@ -0,0 +1,27 @@
+/*
+ * Copyright (c) 2010 Remko Tronçon
+ * Licensed under the GNU General Public License v3.
+ * See Documentation/Licenses/GPLv3.txt for more information.
+ */
+
+#include "SwifTools/Application/WindowsApplicationPathProvider.h"
+
+#include <windows.h>
+
+#include "Swiften/Base/ByteArray.h"
+
+namespace Swift {
+
+WindowsApplicationPathProvider::WindowsApplicationPathProvider(const String& name) : ApplicationPathProvider(name) {
+ resourceDirs.push_back(getExecutableDir());
+ resourceDirs.push_back(getExecutableDir() / "../resources"); // Development
+}
+
+boost::filesystem::path WindowsApplicationPathProvider::getExecutableDir() const {
+ ByteArray data;
+ data.resize(2048);
+ GetModuleFileName(NULL, data.getData(), data.getSize());
+ return boost::filesystem::path(data.toString().getUTF8Data()).parent_path();
+}
+
+}
diff --git a/SwifTools/Application/WindowsApplicationPathProvider.h b/SwifTools/Application/WindowsApplicationPathProvider.h
new file mode 100644
index 0000000..b3341e3
--- /dev/null
+++ b/SwifTools/Application/WindowsApplicationPathProvider.h
@@ -0,0 +1,38 @@
+/*
+ * Copyright (c) 2010 Remko Tronçon
+ * Licensed under the GNU General Public License v3.
+ * See Documentation/Licenses/GPLv3.txt for more information.
+ */
+
+#pragma once
+
+#include "SwifTools/Application/ApplicationPathProvider.h"
+
+namespace Swift {
+ class WindowsApplicationPathProvider : public ApplicationPathProvider {
+ public:
+ WindowsApplicationPathProvider(const String& name);
+
+ boost::filesystem::path getDataDir() const {
+ char* appDirRaw = getenv("APPDATA");
+ boost::filesystem::path result(boost::filesystem::path(appDirRaw) / getApplicationName().getUTF8String());
+ boost::filesystem::create_directory(result);
+ return result;
+ }
+
+ boost::filesystem::path getHomeDir() const {
+ //FIXME: This should be My Documents
+
+ char* homeDirRaw = getenv("USERPROFILE");
+ return boost::filesystem::path(homeDirRaw);
+ }
+
+ virtual boost::filesystem::path getExecutableDir() const;
+ virtual std::vector<boost::filesystem::path> getResourceDirs() const {
+ return resourceDirs;
+ }
+
+ private:
+ std::vector<boost::filesystem::path> resourceDirs;
+ };
+}