← Back to team overview

linuxdcpp-team team mailing list archive

[Branch ~dcplusplus-team/dcplusplus/trunk] Rev 3305: plugin API: add get_language

 

------------------------------------------------------------
revno: 3305
committer: poy <poy@xxxxxxxxxx>
branch nick: trunk
timestamp: Sat 2013-06-01 15:48:06 +0200
message:
  plugin API: add get_language
modified:
  dcpp/PluginApiImpl.cpp
  dcpp/PluginApiImpl.h
  dcpp/PluginDefs.h
  dcpp/Util.cpp
  dcpp/Util.h
  win32/WinUtil.cpp


--
lp:dcplusplus
https://code.launchpad.net/~dcplusplus-team/dcplusplus/trunk

Your team Dcplusplus-team is subscribed to branch lp:dcplusplus.
To unsubscribe from this branch go to https://code.launchpad.net/~dcplusplus-team/dcplusplus/trunk/+edit-subscription
=== modified file 'dcpp/PluginApiImpl.cpp'
--- dcpp/PluginApiImpl.cpp	2013-05-30 18:51:05 +0000
+++ dcpp/PluginApiImpl.cpp	2013-06-01 13:48:06 +0000
@@ -92,14 +92,15 @@
 	DCINTF_CONFIG_VER,
 
 	&PluginApiImpl::getPath,
+	&PluginApiImpl::getInstallPath,
 
 	&PluginApiImpl::setConfig,
 	&PluginApiImpl::getConfig,
 
+	&PluginApiImpl::getLanguage,
+
 	&PluginApiImpl::copyData,
-	&PluginApiImpl::releaseData,
-
-	&PluginApiImpl::getInstallPath
+	&PluginApiImpl::releaseData
 };
 
 DCLog PluginApiImpl::dcLog = {
@@ -149,11 +150,10 @@
 	&PluginApiImpl::removeDownload,
 
 	&PluginApiImpl::setPriority,
+	&PluginApiImpl::pause,
 
 	&PluginApiImpl::copyData,
-	&PluginApiImpl::releaseData,
-
-	&PluginApiImpl::pause
+	&PluginApiImpl::releaseData
 };
 
 DCUtils PluginApiImpl::dcUtils = {
@@ -275,6 +275,12 @@
 	return Util::getPath(static_cast<Util::Paths>(type)).c_str();
 }
 
+ConfigStrPtr DCAPI PluginApiImpl::getInstallPath(const char* guid) {
+	auto str = PluginManager::getInstallPath(guid);
+	ConfigStr value = { CFG_TYPE_STRING, str.c_str() };
+	return reinterpret_cast<ConfigStrPtr>(copyData(reinterpret_cast<ConfigValuePtr>(&value)));
+}
+
 void PluginApiImpl::setConfig(const char* guid, const char* setting, ConfigValuePtr val) {
 	PluginManager* pm = PluginManager::getInstance();
 	switch(val->type) {
@@ -364,6 +370,12 @@
 	return NULL;
 }
 
+ConfigStrPtr DCAPI PluginApiImpl::getLanguage() {
+	auto str = Util::getIETFLang();
+	ConfigStr value = { CFG_TYPE_STRING, str.c_str() };
+	return reinterpret_cast<ConfigStrPtr>(copyData(reinterpret_cast<ConfigValuePtr>(&value)));
+}
+
 ConfigValuePtr PluginApiImpl::copyData(const ConfigValuePtr val) {
 	switch(val->type) {
 		case CFG_TYPE_STRING: {
@@ -433,12 +445,6 @@
 	}
 }
 
-ConfigStrPtr DCAPI PluginApiImpl::getInstallPath(const char* guid) {
-	auto str = PluginManager::getInstallPath(guid);
-	ConfigStr value = { CFG_TYPE_STRING, str.c_str() };
-	return reinterpret_cast<ConfigStrPtr>(copyData(reinterpret_cast<ConfigValuePtr>(&value)));
-}
-
 // Functions for DCLog
 void PluginApiImpl::log(const char* msg) {
 	LogManager::getInstance()->message(msg);

=== modified file 'dcpp/PluginApiImpl.h'
--- dcpp/PluginApiImpl.h	2013-04-23 17:41:14 +0000
+++ dcpp/PluginApiImpl.h	2013-06-01 13:48:06 +0000
@@ -68,10 +68,10 @@
 
 	// Functions For DCConfig
 	static const char* DCAPI getPath(PathType type);
+	static ConfigStrPtr DCAPI getInstallPath(const char* guid);
 	static void DCAPI setConfig(const char* guid, const char* setting, ConfigValuePtr val);
 	static ConfigValuePtr DCAPI getConfig(const char* guid, const char* setting, ConfigType type);
-
-	static ConfigStrPtr DCAPI getInstallPath(const char* guid);
+	static ConfigStrPtr DCAPI getLanguage();
 
 	// Functions for DCLog
 	static void DCAPI log(const char* msg);
@@ -106,7 +106,6 @@
 	static void DCAPI removeDownload(QueueDataPtr qi);
 
 	static void DCAPI setPriority(QueueDataPtr qi, QueuePrio priority);
-
 	static Bool DCAPI pause(QueueDataPtr qi);
 
 	// Functions for DCHub

=== modified file 'dcpp/PluginDefs.h'
--- dcpp/PluginDefs.h	2013-05-30 18:51:05 +0000
+++ dcpp/PluginDefs.h	2013-06-01 13:48:06 +0000
@@ -58,20 +58,20 @@
 
 /* Recommended interfaces */
 #define DCINTF_CONFIG				"generic.plugins.DCConfig"	/* Config management */
-#define DCINTF_CONFIG_VER			2
+#define DCINTF_CONFIG_VER			1
 
 #define DCINTF_LOGGING				"generic.plugins.DCLog"		/* Logging functions */
 #define DCINTF_LOGGING_VER			1
 
 /* Optional interfaces */
 #define DCINTF_DCPP_CONNECTIONS		"dcpp.network.DCConnection"	/* Peer connections */
-#define DCINTF_DCPP_CONNECTIONS_VER	2
+#define DCINTF_DCPP_CONNECTIONS_VER	1
 
 #define DCINTF_DCPP_HUBS			"dcpp.network.DCHub"		/* Hubs */
 #define DCINTF_DCPP_HUBS_VER		1
 
 #define DCINTF_DCPP_QUEUE			"dcpp.queue.DCQueue"		/* Download Queue */
-#define DCINTF_DCPP_QUEUE_VER		2
+#define DCINTF_DCPP_QUEUE_VER		1
 
 #define DCINTF_DCPP_UTILS			"dcpp.utils.DCUtils"		/* Utility and convenience functions */
 #define DCINTF_DCPP_UTILS_VER		1
@@ -339,15 +339,16 @@
 	uint32_t apiVersion;
 
 	const char*			(DCAPI *get_path)			(PathType type);
+	ConfigStrPtr		(DCAPI *get_install_path)	(const char* guid);
 
 	void				(DCAPI *set_cfg)			(const char* guid, const char* setting, ConfigValuePtr val);
 	ConfigValuePtr		(DCAPI *get_cfg)			(const char* guid, const char* setting, ConfigType type);
 
+	/* Return the language used by the host as an IETF language tag. */
+	ConfigStrPtr		(DCAPI *get_language)		();
+
 	ConfigValuePtr		(DCAPI *copy)				(const ConfigValuePtr val);
 	void				(DCAPI *release)			(ConfigValuePtr val);
-
-	/* Version 2 functions */
-	ConfigStrPtr	(DCAPI *get_install_path)	(const char* guid);
 } DCConfig, *DCConfigPtr;
 
 /* Logging functions */
@@ -369,7 +370,6 @@
 	void			(DCAPI *send_protocol_cmd)		(ConnectionDataPtr hConn, const char* cmd);
 	void			(DCAPI *terminate_conn)			(ConnectionDataPtr hConn, Bool graceless);
 
-	/* Version 2 functions */
 	UserDataPtr		(DCAPI *get_user)				(ConnectionDataPtr hConn);
 } DCConnection, *DCConnectionPtr;
 
@@ -408,12 +408,10 @@
 	void			(DCAPI *remove_download)		(QueueDataPtr hItem);
 
 	void			(DCAPI *set_priority)			(QueueDataPtr hItem, QueuePrio priority);
+	Bool			(DCAPI *pause)					(QueueDataPtr hItem);
 
 	QueueDataPtr	(DCAPI *copy)					(const QueueDataPtr hItem);
 	void			(DCAPI *release)				(QueueDataPtr hCopy);
-
-	/* Version 2 functions */
-	Bool			(DCAPI *pause)					(QueueDataPtr hItem);
 } DCQueue, *DCQueuePtr;
 
 /* Utility and convenience functions */

=== modified file 'dcpp/Util.cpp'
--- dcpp/Util.cpp	2013-04-28 20:52:23 +0000
+++ dcpp/Util.cpp	2013-06-01 13:48:06 +0000
@@ -76,6 +76,9 @@
 	dcdebug("bzip2 internal error: %d\n", errcode);
 }
 
+// def taken from <gettextP.h>
+extern "C" const char *_nl_locale_name_default(void);
+
 #ifdef _WIN32
 
 typedef HRESULT (WINAPI* _SHGetKnownFolderPath)(GUID& rfid, DWORD dwFlags, HANDLE hToken, PWSTR *ppszPath);
@@ -247,6 +250,25 @@
 	}
 }
 
+string Util::getIETFLang() {
+	auto lang = SETTING(LANGUAGE);
+	if(lang.empty()) {
+		lang = _nl_locale_name_default();
+	}
+	if(lang.empty() || lang == "C") {
+		lang = "en-US";
+	}
+
+	// replace separation signs by hyphens.
+	size_t i = 0;
+	while((i = lang.find_first_of("_@.", i)) != string::npos) {
+		lang[i] = '-';
+		++i;
+	}
+
+	return lang;
+}
+
 #ifdef _WIN32
 static const char badChars[] = {
 	1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,

=== modified file 'dcpp/Util.h'
--- dcpp/Util.h	2013-03-21 23:03:56 +0000
+++ dcpp/Util.h	2013-06-01 13:48:06 +0000
@@ -151,6 +151,9 @@
 	/** Notepad filename */
 	static string getNotepadFile() { return getPath(PATH_NOTEPAD); }
 
+	/** IETF language tag of the language currently in use. */
+	static string getIETFLang();
+
 	static string translateError(int aError);
 
 	static string getFilePath(const string& path, char separator = PATH_SEPARATOR) {

=== modified file 'win32/WinUtil.cpp'
--- win32/WinUtil.cpp	2013-04-23 17:57:04 +0000
+++ win32/WinUtil.cpp	2013-06-01 13:48:06 +0000
@@ -88,9 +88,6 @@
 using dwt::SaveDialog;
 using dwt::Widget;
 
-// def taken from <gettextP.h>
-extern "C" const char *_nl_locale_name_default(void);
-
 WinUtil::Notification WinUtil::notifications[NOTIFICATION_LAST] = {
 	{ SettingsManager::SOUND_FINISHED_DL, SettingsManager::BALLOON_FINISHED_DL, N_("Download finished"), IDI_DOWNLOAD },
 	{ SettingsManager::SOUND_FINISHED_FL, SettingsManager::BALLOON_FINISHED_FL, N_("File list downloaded"), IDI_DIRECTORY },
@@ -336,20 +333,18 @@
 
 void WinUtil::initHelpPath() {
 	// find the current locale
-	string lang = SETTING(LANGUAGE);
-	if(lang.empty())
-		lang = _nl_locale_name_default();
+	auto lang = Util::getIETFLang();
 
 	// find the path to the help file
 	string path;
-	if(!lang.empty() && lang != "C") {
+	if(lang != "en" && lang != "en-US") {
 		while(true) {
 			path = Util::getPath(Util::PATH_LOCALE) + lang
 				+ PATH_SEPARATOR_STR "help" PATH_SEPARATOR_STR "DCPlusPlus.chm";
 			if(File::getSize(path) != -1)
 				break;
-			// if the lang has extra information (after '_' or '@'), try to remove it
-			string::size_type pos = lang.find_last_of("_@");
+			// if the lang has extra information, try to remove it
+			string::size_type pos = lang.rfind('-');
 			if(pos == string::npos)
 				break;
 			lang = lang.substr(0, pos);