plover/util.c
changeset 24 2b9f54d14cc2
parent 13 b0a35bae4961
child 31 a53fcb780468
     1.1 --- a/plover/util.c	Thu Nov 10 11:15:09 2011 +0000
     1.2 +++ b/plover/util.c	Sat Nov 15 19:04:45 2014 +0000
     1.3 @@ -1,5 +1,5 @@
     1.4  /*
     1.5 - * Copyright (C) 2009, 2011  J. Ali Harlow <ali@juiblex.co.uk>
     1.6 + * Copyright (C) 2009, 2011, 2014  J. Ali Harlow <ali@juiblex.co.uk>
     1.7   *
     1.8   * This program is free software; you can redistribute it and/or modify
     1.9   * it under the terms of the GNU General Public License as published by
    1.10 @@ -27,37 +27,7 @@
    1.11  #include "config.h"
    1.12  #include "plover.h"
    1.13  
    1.14 -char *plover_strconcat(const char *string,...)
    1.15 -{
    1.16 -    va_list ap,aq;
    1.17 -    size_t n;
    1.18 -    char *result=NULL,*t;
    1.19 -    const char *s;
    1.20 -    if (string)
    1.21 -    {
    1.22 -	va_start(ap,string);
    1.23 -	va_copy(aq,ap);
    1.24 -	n=strlen(string);
    1.25 -	while((s=va_arg(aq,const char *)))
    1.26 -	    n+=strlen(s);
    1.27 -	va_end(aq);
    1.28 -	result=malloc(n+1);
    1.29 -	if (result)
    1.30 -	{
    1.31 -	    strcpy(result,string);
    1.32 -	    t=result+strlen(result);
    1.33 -	    while((s=va_arg(ap,const char *)))
    1.34 -	    {
    1.35 -		strcpy(t,s);
    1.36 -		t+=strlen(t);
    1.37 -	    }
    1.38 -	}
    1.39 -	va_end(ap);
    1.40 -    }
    1.41 -    return result;
    1.42 -}
    1.43 -
    1.44 -char *plover_default_prefix_for_vendor(const char *vendor)
    1.45 +gchar *plover_default_prefix_for_vendor(const char *vendor)
    1.46  {
    1.47  #ifdef WIN32
    1.48      /*
    1.49 @@ -83,12 +53,59 @@
    1.50  	  buf);
    1.51  	program_files=buf;
    1.52      }
    1.53 -    return plover_strconcat(program_files,"\\",vendor?vendor:"Plover",NULL);
    1.54 +    return g_strconcat(program_files,"\\",vendor?vendor:"Plover",NULL);
    1.55  #else
    1.56      return NULL;
    1.57  #endif
    1.58  }
    1.59  
    1.60 +gchar *plover_pre_install_prefix(void)
    1.61 +{
    1.62 +#ifdef WIN32
    1.63 +    {
    1.64 +	HRESULT result;
    1.65 +	HKEY key;
    1.66 +	DWORD type,nb;
    1.67 +	int len;
    1.68 +	gunichar2 *str2;
    1.69 +	gchar *root=NULL;
    1.70 +	result=RegOpenKeyW(HKEY_LOCAL_MACHINE,L"Software\\Plover",&key);
    1.71 +	if (result==ERROR_SUCCESS)
    1.72 +	{
    1.73 +	    result=RegQueryValueExW(key,L"Root",0,&type,0,&nb);
    1.74 +	    if (result==ERROR_SUCCESS && type==REG_SZ)
    1.75 +	    {
    1.76 +		str2=malloc(nb);
    1.77 +		result=RegQueryValueExW(key,L"Root",0,NULL,(void *)str2,&nb);
    1.78 +		len=nb/2;
    1.79 +		if (!str2[len-1])       /* Cope with unterminated strings */
    1.80 +		    len--;
    1.81 +		root=g_utf16_to_utf8(str2,len,NULL,NULL,NULL);
    1.82 +		free(str2);
    1.83 +	    }
    1.84 +	    RegCloseKey(key);
    1.85 +	}
    1.86 +	if (!root)
    1.87 +	{
    1.88 +	    root=plover_default_prefix_for_vendor("Plover Root");
    1.89 +	    result=RegCreateKeyExW(HKEY_LOCAL_MACHINE,L"Software\\Plover",0,
    1.90 +	      NULL,REG_OPTION_NON_VOLATILE,KEY_READ|KEY_WRITE,NULL,&key,NULL);
    1.91 +	    if (result==ERROR_SUCCESS)
    1.92 +	    {
    1.93 +		str2=g_utf8_to_utf16(root,-1,NULL,NULL,NULL);
    1.94 +		RegSetValueExW(key,L"Root",0,REG_SZ,(void *)str2,
    1.95 +		  (strlen(root)+1)*sizeof(gunichar2));
    1.96 +		g_free(str2);
    1.97 +		RegCloseKey(key);
    1.98 +	    }
    1.99 +	}
   1.100 +	return root;
   1.101 +    }
   1.102 +#else
   1.103 +    return g_strdup("/var/lib/plover/root");
   1.104 +#endif
   1.105 +}
   1.106 +
   1.107  /*
   1.108   * Get the directory containing the program executable.
   1.109   */
   1.110 @@ -119,3 +136,80 @@
   1.111  	return strdup(".");
   1.112  #endif
   1.113  }
   1.114 +
   1.115 +G_DEFINE_QUARK(plover-razor-error-quark,plover_razor_error)
   1.116 +G_DEFINE_QUARK(plover-mswin-error-quark,plover_mswin_error)
   1.117 +G_DEFINE_QUARK(plover-posix-error-quark,plover_posix_error)
   1.118 +G_DEFINE_QUARK(plover-zlib-error-quark,plover_zlib_error)
   1.119 +
   1.120 +void plover_propagate_razor_error_dup(GError **dest,struct razor_error *src)
   1.121 +{
   1.122 +    GQuark domain;
   1.123 +    int code;
   1.124 +    if (dest)
   1.125 +    {
   1.126 +	code=razor_error_get_code(src);
   1.127 +	switch(razor_error_get_domain(src))
   1.128 +	{
   1.129 +	    case RAZOR_GENERAL_ERROR:
   1.130 +		domain=PLOVER_RAZOR_ERROR;
   1.131 +		break;
   1.132 +	    case RAZOR_POSIX_ERROR:
   1.133 +		domain=PLOVER_POSIX_ERROR;
   1.134 +		break;
   1.135 +	    case RAZOR_MSWIN_ERROR:
   1.136 +		domain=PLOVER_MSWIN_ERROR;
   1.137 +		break;
   1.138 +	    case RAZOR_ZLIB_ERROR:
   1.139 +		domain=PLOVER_ZLIB_ERROR;
   1.140 +		break;
   1.141 +	    case PLOVER_GENERAL_ERROR:
   1.142 +		if (code==PLOVER_GENERAL_ERROR_CANCELLED)
   1.143 +		{
   1.144 +		    domain=G_IO_ERROR;
   1.145 +		    code=G_IO_ERROR_CANCELLED;
   1.146 +		    break;
   1.147 +		}
   1.148 +		/* else fall though */
   1.149 +	    default:
   1.150 +		domain=PLOVER_RAZOR_ERROR;
   1.151 +		code=RAZOR_GENERAL_ERROR_FAILED;
   1.152 +	}
   1.153 +	*dest=g_error_new_literal(domain,code,razor_error_get_msg(src));
   1.154 +    }
   1.155 +}
   1.156 +
   1.157 +void plover_propagate_razor_error(GError **dest,struct razor_error *src)
   1.158 +{
   1.159 +    plover_propagate_razor_error_dup(dest,src);
   1.160 +    razor_error_free(src);
   1.161 +}
   1.162 +
   1.163 +void plover_propagate_g_error(struct razor_error **dest,GError *src)
   1.164 +{
   1.165 +    int domain,code;
   1.166 +    if (dest)
   1.167 +    {
   1.168 +	code=src->code;
   1.169 +	if (src->domain==PLOVER_RAZOR_ERROR)
   1.170 +	    domain=RAZOR_GENERAL_ERROR;
   1.171 +	else if (src->domain==PLOVER_POSIX_ERROR)
   1.172 +	    domain=RAZOR_POSIX_ERROR;
   1.173 +	else if (src->domain==PLOVER_MSWIN_ERROR)
   1.174 +	    domain=RAZOR_MSWIN_ERROR;
   1.175 +	else if (src->domain==PLOVER_ZLIB_ERROR)
   1.176 +	    domain=RAZOR_ZLIB_ERROR;
   1.177 +	else if (src->domain==G_IO_ERROR && code==G_IO_ERROR_CANCELLED)
   1.178 +	{
   1.179 +	    domain=PLOVER_GENERAL_ERROR;
   1.180 +	    code=PLOVER_GENERAL_ERROR_CANCELLED;
   1.181 +	}
   1.182 +	else
   1.183 +	{
   1.184 +	    domain=RAZOR_GENERAL_ERROR;
   1.185 +	    code=RAZOR_GENERAL_ERROR_FAILED;
   1.186 +	}
   1.187 +	*dest=razor_error_new_str(domain,code,NULL,src->message);
   1.188 +    }
   1.189 +    g_error_free(src);
   1.190 +}