Пример #1
0
// WIN32
static const gchar* create_fsel_3(const gchar *dirname, const gchar *filename, const gchar *ext, gboolean save)
{
#ifdef WIN32
	OPENFILENAME_MAYALIAS o;
	char lpstrFile[2048] = "\0";
	char lpstrFilter[512];
	char *p;
	gchar **sarray;
	int i, n;
	int have_widechar = G_WIN32_HAVE_WIDECHAR_API();
	void *sdirname;

	// clear structure
	memset (&o, 0, sizeof(OPENFILENAME));

	// set default filename
	if(filename)
	{
		void *temp;
		if (have_widechar)
		{
			temp = g_utf8_to_utf16(filename,-1,NULL,NULL,NULL);
			if(!temp) return NULL;
			wcsncpy((wchar_t *)lpstrFile, temp, sizeof(lpstrFile)>>1);
		}
		else
		{
			temp = g_locale_from_utf8(filename,-1,NULL,NULL,NULL);
			if(!temp) return NULL;
			strncpy(lpstrFile, temp, sizeof(lpstrFile));
		}
		g_free(temp);
	}
Пример #2
0
/**
 * g_win32_error_message:
 * @error: error code.
 *
 * Translate a Win32 error code (as returned by GetLastError()) into
 * the corresponding message. The message is either language neutral,
 * or in the thread's language, or the user's language, the system's
 * language, or US English (see docs for FormatMessage()). The
 * returned string is in UTF-8. It should be deallocated with
 * g_free().
 *
 * Returns: newly-allocated error message
 **/
static gchar *
g_win32_error_message (gint error)
{
  gchar *retval;

  if (G_WIN32_HAVE_WIDECHAR_API ())
    {
      wchar_t *msg = NULL;
      int nchars;

      FormatMessageW (FORMAT_MESSAGE_ALLOCATE_BUFFER
		      |FORMAT_MESSAGE_IGNORE_INSERTS
		      |FORMAT_MESSAGE_FROM_SYSTEM,
		      NULL, error, 0,
		      (LPWSTR) &msg, 0, NULL);
      if (msg != NULL)
	{
	  nchars = wcslen (msg);

	  if (nchars > 2 && msg[nchars-1] == '\n' && msg[nchars-2] == '\r')
	    msg[nchars-2] = '\0';
	  
	  retval = g_utf16_to_utf8 (msg, -1, NULL, NULL, NULL);
	  
	  LocalFree (msg);
	}
      else
	retval = g_strdup ("");
    }
  else
    {
      gchar *msg = NULL;
      int nbytes;

      FormatMessageA (FORMAT_MESSAGE_ALLOCATE_BUFFER
		      |FORMAT_MESSAGE_IGNORE_INSERTS
		      |FORMAT_MESSAGE_FROM_SYSTEM,
		      NULL, error, 0,
		      (LPTSTR) &msg, 0, NULL);
      if (msg != NULL)
	{
	  nbytes = strlen (msg);

	  if (nbytes > 2 && msg[nbytes-1] == '\n' && msg[nbytes-2] == '\r')
	    msg[nbytes-2] = '\0';
	  
	  retval = g_locale_to_utf8 (msg, -1, NULL, NULL, NULL);
	  
	  LocalFree (msg);
	}
      else
	retval = g_strdup ("");
    }

  return retval;
}
Пример #3
0
static void
open_button_cb(GtkButton *button, GaimGtkXferDialog *dialog)
{
#ifdef _WIN32 /* Only supported in Win32 right now */
	int code;
	if (G_WIN32_HAVE_WIDECHAR_API ()) {
		wchar_t *wc_filename = g_utf8_to_utf16(
				gaim_xfer_get_local_filename(
					dialog->selected_xfer),
				-1, NULL, NULL, NULL);
	
		code = (int) ShellExecuteW(NULL, NULL, wc_filename, NULL, NULL,
				SW_SHOW);

		g_free(wc_filename);
	} else {
		char *l_filename = g_locale_from_utf8(
				gaim_xfer_get_local_filename(
					dialog->selected_xfer),
				-1, NULL, NULL, NULL);
	
		code = (int) ShellExecuteA(NULL, NULL, l_filename, NULL, NULL,
				SW_SHOW);

		g_free(l_filename);
	}

	if (code == SE_ERR_ASSOCINCOMPLETE || code == SE_ERR_NOASSOC)
	{
		gaim_notify_error(NULL, NULL,
				_("There is no application configured to open this type of file."), NULL);
	}
	else if (code < 32)
	{
		gaim_notify_error(NULL, NULL,
				_("An error occurred while opening the file."), NULL);
		gaim_debug_warning("ft", "filename: %s; code: %d\n",
				gaim_xfer_get_local_filename(dialog->selected_xfer), code);
	}
#endif
}
Пример #4
0
/**
 * ticonv_utf16_to_gfe:
 * @model: a calculator model taken in #CalcModel.
 * @src: the name of variable to convert from UTF-16
 *
 * This function converts a varname into a valid filename (depends on locale).
 * Example: 'foobar' => foobar, 'alpha' => _alpha_.
 * 
 * Greeks characters need conversion if the locale is not UTF-8 (Windows for sure, Linux
 * if locale is different of UTF-8) because greek characters are often missed or mis-converted
 * when converting to locale.
 *
 * Return value: %dst as a newly allocated string.
 **/ 
TIEXPORT4 char* TICALL ticonv_utf16_to_gfe(CalcModel model, const unsigned short *src)
{
#ifdef __WIN32__
	int is_utf8 = G_WIN32_HAVE_WIDECHAR_API();
#else
	int is_utf8 = g_get_charset(NULL);
#endif
	const char *str;
	unsigned short *utf16_src, *p;
	unsigned short *utf16_dst, *q;
	char *dst;

	// detokenization to UTF-16
	p = utf16_src = (unsigned short *)src;
	q = utf16_dst = g_malloc0(18*ticonv_utf16_strlen(utf16_src)+2);

	// conversion from UTF-16 to UTF-16
	if(tifiles_calc_is_ti9x(model) && !is_utf8)
	{
		while(*p)
		{
			unsigned long msb = *p & 0xff00;

			if(!msb)
			{
				*q++ = *p++ & 0xff;
			}
			else
			{
				gunichar2 *str2;
				glong ir, iw;

				switch(*p)
				{
					case 0x03bc: str = "_mu_"; break;
					case 0x03b1: str = "_alpha_"; break;
					case 0x03b2: str = "_beta_"; break;
					case 0x0393: str = "_GAMMA_"; break;
					case 0x03b3: str = "_gamma_"; break;
					case 0x0394: str = "_DELTA_"; break;
					case 0x03b4: str = "_delta_"; break;
					case 0x03b5: str = "_epsilon_";break;
					case 0x03b6: str = "_zeta_"; break;
					case 0x03b8: str = "_theta_"; break;
					case 0x03bb: str = "_lambda_"; break;
					case 0x03be: str = "_ksi_"; break;
					case 0x03a0: str = "_PI_"; break;
					case 0x03c0: str = "_pi_"; break;
					case 0x03c1: str = "_rho_"; break;
					case 0x03a3: str = "_SIGMA_"; break; 
					case 0x03c3: str = "_sigma_"; break; 
					case 0x03c4: str = "_tau_"; break;
					case 0x03d5: str = "_PHI_"; break;
					case 0x03a8: str = "_PSI_"; break;
					case 0x03a9: str = "_OMEGA_"; break; 
					case 0x03c9: str = "_omega_"; break;
					default: str = ""; break;
				}

				str2 = g_utf8_to_utf16(str, -1, &ir, &iw, NULL);
				memcpy(q, str2, (iw+1) * sizeof(unsigned short));
				g_free(str2);

				q += iw;
				p++;
			}
		}
		*q = '\0';
	}
	else if(tifiles_calc_is_ti8x(model) && !is_utf8)
	{
		while(*p)
		{
			unsigned long msb = *p & 0xff00;

			if(!msb)
			{
				*q++ = *p++ & 0xff;
			}
			else
			{
				if(*p >= 0x2080 && *p <= 0x2089)
				{
					*q++ = (*p++ - 0x2080) + '0';
				}
				else
				{
					gunichar2 *str2;
				glong ir, iw;

				switch(*p)
				{
					case 0x03bc: str = "_mu_"; break;
					case 0x03b1: str = "_alpha_"; break;
					case 0x03b2: str = "_beta_"; break;
					case 0x0393: str = "_GAMMA_"; break;
					case 0x03b3: str = "_gamma_"; break;
					case 0x0394: str = "_DELTA_"; break;
					case 0x03b4: str = "_delta_"; break;
					case 0x03b5: str = "_epsilon_";break;
					case 0x03b6: str = "_zeta_"; break;
					case 0x03b8: str = "_theta_"; break;
					case 0x03bb: str = "_lambda_"; break;
					case 0x03be: str = "_ksi_"; break;
					case 0x03a0: str = "_PI_"; break;
					case 0x03c0: str = "_pi_"; break;
					case 0x03c1: str = "_rho_"; break;
					case 0x03a3: str = "_SIGMA_"; break; 
					case 0x03c3: str = "_sigma_"; break; 
					case 0x03c4: str = "_tau_"; break;
					case 0x03d5: str = "_PHI_"; break;
					case 0x03a8: str = "_PSI_"; break;
					case 0x03a9: str = "_OMEGA_"; break; 
					case 0x03c9: str = "_omega_"; break;
					default: str = ""; break;
				}

				str2 = g_utf8_to_utf16(str, -1, &ir, &iw, NULL);
				memcpy(q, str2, (iw+1) * sizeof(gunichar2));
				g_free(str2);

				q += iw;
				p++;
				}
			}
		}
		*q = '\0';
	}
	else
	{
		while(*p) 
		{
#ifdef __WIN32__
			if(*p >= 0x2080 && *p <= 0x2089)
			{
				*q++ = (*p++ - 0x2080) + '0';
			}
			else		
#endif
			*q++ = *p++;
		}
		*q = '\0';
	}

	// '/' is not allowed in filenames
	for(q = utf16_dst; *q; q++)
	{
		if(*q == '/') 
			*q = '_';
	}

	// UTF-16 to UTF-8 to GFE encoding
	{
		gchar *utf8;

		utf8 = g_utf16_to_utf8(utf16_dst, -1, NULL, NULL, NULL);
		g_free(utf16_dst);

		dst = g_filename_from_utf8(utf8, -1, NULL, NULL, NULL);
		g_free(utf8);
	}

	return dst;	
}
static void
setup_runtime_paths (void)
{
        char cpbfr[1000];
        wchar_t wcbfr[1000];
        char *full_prefix = NULL;
        char *cp_prefix = NULL;
  
        G_LOCK (mutex);
        if (prefix != NULL) {
                G_UNLOCK (mutex);
                return;
        }

        if (G_WIN32_HAVE_WIDECHAR_API ()) {
                /* NT-based Windows */
                if (GetModuleFileNameW (hmodule, wcbfr, G_N_ELEMENTS (wcbfr))) {
                        full_prefix = g_utf16_to_utf8 (wcbfr, -1,
                                                   NULL, NULL, NULL);
                        if (GetShortPathNameW (wcbfr, wcbfr, G_N_ELEMENTS (wcbfr)) &&
                            WideCharToMultiByte (CP_ACP, 0, wcbfr, -1,
                                                 cpbfr, G_N_ELEMENTS (cpbfr),
                                                 NULL, NULL))
                                cp_prefix = g_strdup (cpbfr);
                        else if (full_prefix)
                                cp_prefix = g_locale_from_utf8 (full_prefix, -1,
                                                                NULL, NULL, NULL);
                }
        } else {
                /* Win9x */
                if (GetModuleFileNameA (hmodule, cpbfr, G_N_ELEMENTS (cpbfr))) {
                        full_prefix = g_locale_to_utf8 (cpbfr, -1,
                                                        NULL, NULL, NULL);
                        cp_prefix = g_strdup (cpbfr);
                }
        }

        if (full_prefix != NULL) {
                gchar *p = strrchr (full_prefix, '\\');
                if (p != NULL)
                        *p = '\0';
      
                p = strrchr (full_prefix, '\\');
                if (p && (g_ascii_strcasecmp (p + 1, "bin") == 0))
                        *p = '\0';
                prefix = full_prefix;
        }
                  
        if (cp_prefix != NULL) {
                gchar *p = _mbsrchr (cp_prefix, '\\');
                if (p != NULL)
                        *p = '\0';
      
                p = _mbsrchr (cp_prefix, '\\');
                if (p && (g_ascii_strcasecmp (p + 1, "bin") == 0))
                        *p = '\0';
                
        }

        server_libexecdir = _matecomponent_activation_win32_replace_prefix (full_prefix, SERVER_LIBEXECDIR);
        serverinfodir = _matecomponent_activation_win32_replace_prefix (full_prefix, SERVERINFODIR);
        server_confdir = _matecomponent_activation_win32_replace_prefix (full_prefix, SERVER_CONFDIR);
        localedir = _matecomponent_activation_win32_replace_prefix (cp_prefix, MATECOMPONENT_ACTIVATION_LOCALEDIR);

        G_UNLOCK (mutex);
}
Пример #6
0
static void
open_button_cb(GtkButton *button, PidginXferDialog *dialog)
{
#ifdef _WIN32
	/* If using Win32... */
	int code;
	if (G_WIN32_HAVE_WIDECHAR_API ()) {
		wchar_t *wc_filename = g_utf8_to_utf16(
				purple_xfer_get_local_filename(
					dialog->selected_xfer),
				-1, NULL, NULL, NULL);

		code = (int) ShellExecuteW(NULL, NULL, wc_filename, NULL, NULL,
				SW_SHOW);

		g_free(wc_filename);
	} else {
		char *l_filename = g_locale_from_utf8(
				purple_xfer_get_local_filename(
					dialog->selected_xfer),
				-1, NULL, NULL, NULL);

		code = (int) ShellExecuteA(NULL, NULL, l_filename, NULL, NULL,
				SW_SHOW);

		g_free(l_filename);
	}

	if (code == SE_ERR_ASSOCINCOMPLETE || code == SE_ERR_NOASSOC)
	{
		purple_notify_error(dialog, NULL,
				_("There is no application configured to open this type of file."), NULL);
	}
	else if (code < 32)
	{
		purple_notify_error(dialog, NULL,
				_("An error occurred while opening the file."), NULL);
		purple_debug_warning("ft", "filename: %s; code: %d\n",
				purple_xfer_get_local_filename(dialog->selected_xfer), code);
	}
#else
	const char *filename = purple_xfer_get_local_filename(dialog->selected_xfer);
	char *command = NULL;
	char *tmp = NULL;
	GError *error = NULL;

	if (purple_running_gnome())
	{
		char *escaped = g_shell_quote(filename);
		command = g_strdup_printf("gnome-open %s", escaped);
		g_free(escaped);
	}
	else if (purple_running_kde())
	{
		char *escaped = g_shell_quote(filename);

		if (purple_str_has_suffix(filename, ".desktop"))
			command = g_strdup_printf("kfmclient openURL %s 'text/plain'", escaped);
		else
			command = g_strdup_printf("kfmclient openURL %s", escaped);
		g_free(escaped);
	}
	else
	{
		purple_notify_uri(NULL, filename);
		return;
	}

	if (purple_program_is_valid(command))
	{
		gint exit_status;
		if (!g_spawn_command_line_sync(command, NULL, NULL, &exit_status, &error))
		{
			tmp = g_strdup_printf(_("Error launching %s: %s"),
							purple_xfer_get_local_filename(dialog->selected_xfer),
							error->message);
			purple_notify_error(dialog, NULL, _("Unable to open file."), tmp);
			g_free(tmp);
			g_error_free(error);
		}
		if (exit_status != 0)
		{
			char *primary = g_strdup_printf(_("Error running %s"), command);
			char *secondary = g_strdup_printf(_("Process returned error code %d"),
									exit_status);
			purple_notify_error(dialog, NULL, primary, secondary);
			g_free(tmp);
		}
	}
#endif
}
Пример #7
0
/**
 * g_file_test:
 * @filename: a filename to test in the GLib file name encoding
 * @test: bitfield of #GFileTest flags
 * 
 * Returns %TRUE if any of the tests in the bitfield @test are
 * %TRUE. For example, <literal>(G_FILE_TEST_EXISTS | 
 * G_FILE_TEST_IS_DIR)</literal> will return %TRUE if the file exists; 
 * the check whether it's a directory doesn't matter since the existence 
 * test is %TRUE. With the current set of available tests, there's no point
 * passing in more than one test at a time.
 * 
 * Apart from %G_FILE_TEST_IS_SYMLINK all tests follow symbolic links,
 * so for a symbolic link to a regular file g_file_test() will return
 * %TRUE for both %G_FILE_TEST_IS_SYMLINK and %G_FILE_TEST_IS_REGULAR.
 *
 * Note, that for a dangling symbolic link g_file_test() will return
 * %TRUE for %G_FILE_TEST_IS_SYMLINK and %FALSE for all other flags.
 *
 * You should never use g_file_test() to test whether it is safe
 * to perform an operation, because there is always the possibility
 * of the condition changing before you actually perform the operation.
 * For example, you might think you could use %G_FILE_TEST_IS_SYMLINK
 * to know whether it is is safe to write to a file without being
 * tricked into writing into a different location. It doesn't work!
 *
 * <informalexample><programlisting>
 * /&ast; DON'T DO THIS &ast;/
 *  if (!g_file_test (filename, G_FILE_TEST_IS_SYMLINK)) {
 *    fd = g_open (filename, O_WRONLY);
 *    /&ast; write to fd &ast;/
 *  }
 * </programlisting></informalexample>
 *
 * Another thing to note is that %G_FILE_TEST_EXISTS and
 * %G_FILE_TEST_IS_EXECUTABLE are implemented using the access()
 * system call. This usually doesn't matter, but if your program
 * is setuid or setgid it means that these tests will give you
 * the answer for the real user ID and group ID, rather than the
 * effective user ID and group ID.
 *
 * On Windows, there are no symlinks, so testing for
 * %G_FILE_TEST_IS_SYMLINK will always return %FALSE. Testing for
 * %G_FILE_TEST_IS_EXECUTABLE will just check that the file exists and
 * its name indicates that it is executable, checking for well-known
 * extensions and those listed in the %PATHEXT environment variable.
 *
 * Return value: whether a test was %TRUE
 **/
gboolean
g_file_test (const gchar *filename,
             GFileTest    test)
{
#ifdef G_OS_WIN32
  int attributes;

  if (G_WIN32_HAVE_WIDECHAR_API ())
    {
      wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, NULL);

      if (wfilename == NULL)
	return FALSE;

      attributes = GetFileAttributesW (wfilename);

      g_free (wfilename);
    }
  else
    {
      gchar *cpfilename = g_locale_from_utf8 (filename, -1, NULL, NULL, NULL);

      if (cpfilename == NULL)
	return FALSE;
      
      attributes = GetFileAttributesA (cpfilename);
      
      g_free (cpfilename);
    }

  if (attributes == INVALID_FILE_ATTRIBUTES)
    return FALSE;

  if (test & G_FILE_TEST_EXISTS)
    return TRUE;
      
  if (test & G_FILE_TEST_IS_REGULAR)
    return (attributes & (FILE_ATTRIBUTE_DIRECTORY | FILE_ATTRIBUTE_DEVICE)) == 0;

  if (test & G_FILE_TEST_IS_DIR)
    return (attributes & FILE_ATTRIBUTE_DIRECTORY) != 0;

  if (test & G_FILE_TEST_IS_EXECUTABLE)
    {
      const gchar *lastdot = strrchr (filename, '.');
      const gchar *pathext = NULL, *p;
      int extlen;

      if (lastdot == NULL)
	return FALSE;

      if (stricmp (lastdot, ".exe") == 0 ||
	  stricmp (lastdot, ".cmd") == 0 ||
	  stricmp (lastdot, ".bat") == 0 ||
	  stricmp (lastdot, ".com") == 0)
	return TRUE;

      /* Check if it is one of the types listed in %PATHEXT% */

      pathext = g_getenv ("PATHEXT");
      if (pathext == NULL)
	return FALSE;

      pathext = g_utf8_casefold (pathext, -1);

      lastdot = g_utf8_casefold (lastdot, -1);
      extlen = strlen (lastdot);

      p = pathext;
      while (TRUE)
	{
	  const gchar *q = strchr (p, ';');
	  if (q == NULL)
	    q = p + strlen (p);
	  if (extlen == q - p &&
	      memcmp (lastdot, p, extlen) == 0)
	    {
	      g_free ((gchar *) pathext);
	      g_free ((gchar *) lastdot);
	      return TRUE;
	    }
	  if (*q)
	    p = q + 1;
	  else
	    break;
	}

      g_free ((gchar *) pathext);
      g_free ((gchar *) lastdot);
      return FALSE;
    }

  return FALSE;
#else
  if ((test & G_FILE_TEST_EXISTS) && (access (filename, F_OK) == 0))
    return TRUE;
  
  if ((test & G_FILE_TEST_IS_EXECUTABLE) && (access (filename, X_OK) == 0))
    {
      if (getuid () != 0)
	return TRUE;

      /* For root, on some POSIX systems, access (filename, X_OK)
       * will succeed even if no executable bits are set on the
       * file. We fall through to a stat test to avoid that.
       */
    }
  else
    test &= ~G_FILE_TEST_IS_EXECUTABLE;

  if (test & G_FILE_TEST_IS_SYMLINK)
    {
      struct stat s;

      if ((lstat (filename, &s) == 0) && S_ISLNK (s.st_mode))
        return TRUE;
    }
  
  if (test & (G_FILE_TEST_IS_REGULAR |
	      G_FILE_TEST_IS_DIR |
	      G_FILE_TEST_IS_EXECUTABLE))
    {
      struct stat s;
      
      if (stat (filename, &s) == 0)
	{
	  if ((test & G_FILE_TEST_IS_REGULAR) && S_ISREG (s.st_mode))
	    return TRUE;
	  
	  if ((test & G_FILE_TEST_IS_DIR) && S_ISDIR (s.st_mode))
	    return TRUE;

	  /* The extra test for root when access (file, X_OK) succeeds.
	   */
	  if ((test & G_FILE_TEST_IS_EXECUTABLE) &&
	      ((s.st_mode & S_IXOTH) ||
	       (s.st_mode & S_IXUSR) ||
	       (s.st_mode & S_IXGRP)))
	    return TRUE;
	}
    }

  return FALSE;
#endif
}