Logo Search packages:      
Sourcecode: tea version File versions  Download package

tea_funx.c

/***************************************************************************
                          tea_funx.c  -  description
                             -------------------
    begin                : Mon Dec 12 2003
    copyright            : (C) 2003, 2004, 2005 by Peter 'Roxton' Semiletov
    email                : peter.semiletov@gmail.com
 ***************************************************************************/

/*
Copyright (C) 2002 Paolo Maggi
Copyright (C) 2000 Olivier Sessink
Copyright (C) 2000  Kh. Naba Kumar Singh
*/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/

#ifdef HAVE_CONFIG_H
#  include <config.h>
#endif

#include <unistd.h>

#include <gtk/gtk.h>
#include <gdk/gdk.h>
#include <gdk/gdkkeysyms.h>
#include <gdk-pixbuf/gdk-pixbuf.h>
#include <glib/gi18n.h>
#include <errno.h>
#include <sys/stat.h>
#include <time.h>
#include <stdio.h>

#include "tea_defs.h"
#include "tea_config.h"
#include "rox_strings.h"
#include "interface.h"
#include "tea_text_document.h"
#include "tea_funx.h"
#include "tea_proj.h"
#include "tea_gtk_utils.h"
#include "callbacks.h"


/* from:
 * time.c
 * This file is part of gedit
 *
 * Copyright (C) 2002 Paolo Maggi

  changed by roxton
*/

gchar* get_time (const gchar* format)
{
   if (! format)
      return NULL;

      gchar *out = NULL;
      gchar *out_utf8 = NULL;
      time_t clock;
      struct tm *now;
      size_t out_length = 0;
      gchar *locale_format;

      if (strlen (format) == 0)
            return g_strdup (" ");

      locale_format = g_locale_from_utf8 (format, -1, NULL, NULL, NULL);

      if (! locale_format)
            return g_strdup (" ");

      clock = time (NULL);
      now = localtime (&clock);

      do
      {
            out_length += 255;
            out = g_realloc (out, out_length);
      }
      while (strftime (out, out_length, locale_format, now) == 0);

      g_free (locale_format);

      if (g_utf8_validate (out, -1, NULL))
            out_utf8 = out;
      else
      {
            out_utf8 = g_locale_to_utf8 (out, -1, NULL, NULL, NULL);
            g_free (out);

            if (! out_utf8)
                  out_utf8 = g_strdup (" ");
      }

      return out_utf8;
}


//from Anjuta ::
//    utilities.c
//  Copyright (C) 2000  Kh. Naba Kumar Singh
#define FILE_BUFFER_SIZE 65535
gboolean
copy_file (gchar * src, gchar * dest)
{
      FILE *input_fp, *output_fp;
      gchar buffer[FILE_BUFFER_SIZE];
      gint bytes_read, bytes_written;
      gboolean error;

      error = TRUE;

      input_fp = fopen (src, "rb");
      if (input_fp == NULL)
      {
            return FALSE;
      }

      output_fp = fopen (dest, "wb");
      if (output_fp == NULL)
      {
            fclose (input_fp);
            return TRUE;
      }

      for (;;)
      {
            bytes_read = fread (buffer, 1, FILE_BUFFER_SIZE, input_fp);
            if (bytes_read != FILE_BUFFER_SIZE && ferror (input_fp))
            {
                  error = FALSE;
                  break;
            }

            if (bytes_read)
            {
                  bytes_written = fwrite (buffer, 1, bytes_read, output_fp);
                  if (bytes_read != bytes_written)
                  {
                        error = FALSE;
                        break;
                  }
            }

            if (bytes_read != FILE_BUFFER_SIZE && feof (input_fp))
            {
                  break;
            }
      }

      fclose (input_fp);
      fclose (output_fp);

      return error;
}
//


//from Bluefish:: bf_lib.c and gtk_easy
/**
 * ending_slash:
 * @dirname: a #const gchar * with a diretory name
 *
 * makes sure the last character of the newly allocated
 * string it returns is a '/'
 *
 * Return value: a newly allocated gchar * dirname that does end on a '/'
 **/
gchar *ending_slash(const gchar *dirname) {
      if (!dirname) {
            return g_strdup("");
      }

      if (dirname[strlen(dirname)-1] == G_DIR_SEPARATOR) {
            return g_strdup(dirname);
      } else {
            return g_strconcat(dirname, G_DIR_SEPARATOR_S, NULL);
      }
}


/**
 * most_efficient_filename:
 * @filename: a gchar * with a possibly inefficient filename like /hello/../tmp/../myfile
 *
 * tries to eliminate any dir/../ combinations in filename
 * this function could do evern better, it should also remove /./ entries
 *
 * Return value: the same gchar * as passed to the function
 **/
gchar *most_efficient_filename(gchar *filename) {
      gint i,j, len;

      len = strlen(filename);
      for (i=0; i < len-4; i++) {
            if (strncmp(&filename[i], "/../", 4) == 0) {
                  for (j=1; j < i; j++) {
                        if ((filename[i - j] == G_DIR_SEPARATOR) || (i==j)) {
                              memmove(&filename[i-j], &filename[i+3], len-i);
                              j=i;
                              i--;
                        }
                  }
            }
      }
      return filename;
}


/**
 * create_full_path:
 * @filename: a gchar * with the (relative or not) filename
 * @basedir: a gchar * with a basedir or NULL for current dir
 *
 * if filename is already absolute, it returns it
 * else it will use basedir if available, else the current dir
 * to add to the filename to form the full path
 *
 * it does use most_efficient_filename() to remote unwanted dir/../ entries
 *
 * Return value: a newly allocated gchar * with the full path
 **/


gchar *create_full_path(gchar * filename, gchar *basedir) {
      gchar *absolute_filename;
      gchar *tmpcdir;


      if (g_path_is_absolute(filename)) {
            absolute_filename = g_strdup(filename);
      } else {
            if (basedir) {
                  tmpcdir = ending_slash(basedir);
            } else {
                  gchar *curdir = g_get_current_dir();
                  tmpcdir = ending_slash(curdir);
                  g_free(curdir);
            }
            absolute_filename = g_strconcat(tmpcdir, filename, NULL);
            g_free(tmpcdir);
      }
      absolute_filename = most_efficient_filename(absolute_filename);
      return absolute_filename;
}
//



static gint noname_name_counter = -1;

gchar* get_noname_name (void) 
{
  ++noname_name_counter;
  if (noname_name_counter >= G_MAXINT)
     noname_name_counter = 0;
  return g_strdup_printf ("noname_%d", noname_name_counter);
}


int get_value (int total, int perc)
{
  return (int) (total / 100) * perc;
}


int get_percent (int total, int value)
{
  return (int) (value / total) * 100;
}


void handle_file (gchar *filename, gint mode, gboolean kwas)
{
  if (! filename)
     return;
 
  gchar *cmd = NULL;
  
  gint i = get_n_page_by_filename (filename);

  if (i != -1)
     {
      gtk_notebook_set_current_page (notebook1, i);
      return;
     } 

  if (! g_file_test (filename, G_FILE_TEST_EXISTS))
     return;
 
  if (is_image (filename))
     {
      if (mode != 0)
         {
          cmd = g_strdup_printf (confile.ext_pic_editor, filename);
          system (cmd);
          g_free (cmd);
          return;
         }

      if (confile.use_ext_image_viewer)
         {
          cmd = g_strdup_printf (confile.ext_pic_editor, filename);
          system (cmd);
          g_free(cmd);
          return;        
         }
      else  
          create_wnd_imgviewer (filename);

      return;
     }

  if (! kwas)
     {
      if (! get_page_text() )
        cur_settings.selected_enc = ch_str (cur_settings.selected_enc, confile.default_charset);
      else
          cur_settings.selected_enc = ch_str (cur_settings.selected_enc, cur_text_doc->encoding);
     }

  open_file_std (filename);
}


void handle_file_enc (gchar *filename, gchar *enc)
{
  if (! filename)
     return;
 
  gchar *cmd = NULL;
  
  gint i = get_n_page_by_filename (filename);

  if (i != -1)
     {
      gtk_notebook_set_current_page (notebook1, i);
      return;
     } 

  if (! g_file_test (filename, G_FILE_TEST_EXISTS))
     return;
  
  cur_settings.selected_enc = ch_str (cur_settings.selected_enc, enc);

  open_file_std (filename);
}
      
/*
gboolean is_image2 (const gchar *filename)
{
  return is_ext (filename, ".png", ".gif", ".jpg", ".jpeg", ".wbmp", ".bmp", ".mng", NULL);
}
*/

gboolean is_tex (const gchar *filename)
{
  return is_ext (filename, ".tex", NULL);
}


gboolean is_po (const gchar *f)
{
  return is_ext (f, ".pot", ".po", NULL); 
}


void create_empty_file (gchar *filename, gchar *first_line)
{
  FILE *out = fopen (filename, "w");
  
  if (! out) 
     return FALSE;                     

  if (first_line)
     fprintf (out, "%s", first_line);

  fclose (out);                        
}


gint get_file_size (const gchar *filename)
{
  struct stat s;
  stat (filename, &s);
  return s.st_size;
}


gboolean check_ext_wo_dot (gchar *filename, gchar *ext)
{
  if (! filename || ! ext)
     return FALSE;

  gboolean r = FALSE;
  gchar *f = g_utf8_strdown (filename, -1);

  gchar *full_ext = g_strconcat (".", ext, NULL);

  r = g_str_has_suffix (f, full_ext);
  g_free (full_ext);
  g_free (f);
  return r; 
}


gboolean check_ext (gchar *filename, gchar *ext)
{
  if (! filename || ! ext)
     return FALSE;

  gboolean r = FALSE;
  gchar *f = g_utf8_strdown (filename, -1);
  r = g_str_has_suffix (f, ext);
  g_free (f);
  return r; 
}


gboolean is_markup (const gchar *f)
{
  return is_ext (f, ".html", ".htm", ".shtml", ".xml", ".sgm", ".xhtml", ".docbook", NULL);
}


gboolean is_c (const gchar *f)
{
  return is_ext (f, ".c", ".h", ".cpp", ".cxx", ".cc", ".cp", ".c++", NULL);
}


gboolean is_pascal (const gchar *f)
{
  return is_ext (f, ".pas", ".pp", ".dpr", NULL);
}


gchar* get_hl_name (gchar *file_name)
{
  gchar *b;

  if (is_markup (file_name))
     return g_strdup (HL_MARKUP); 
       
  if (is_c (file_name))
     return g_strdup (HL_C); 
 
  if (is_pascal (file_name))
     return g_strdup (HL_PASCAL); 

  if (check_ext (file_name, ".py")) 
     return g_strdup (HL_PYTHON); 
 
  if (check_ext (file_name, ".php")) 
     return g_strdup (HL_PHP); 

  if (check_ext (file_name, ".sh")) 
     return g_strdup (HL_BASH); 

  if (is_po (file_name))
     return g_strdup (HL_PO); 

  if (is_tex (file_name))
     return g_strdup (HL_TEX); 

  if (confile.do_det_scripts_by_content)
     {
      b = str_file_read (file_name);
      if (b)
         { 
          if (strstr (b, "#!/bin/bash") || strstr (b, "#!/bin/sh"))
             {
              g_free (b);
              return g_strdup (HL_BASH); 
             }
          g_free (b);
         }
     } 

  return g_strdup (HL_NONE);
}


gchar* get_l_filename (const gchar *filename)
{
  if (! filename)
     return NULL;

  gsize bytes_read;
  gsize bytes_written;
  return g_filename_from_utf8 (filename, -1, &bytes_read, &bytes_written, NULL);  
}


gchar* get_8_filename (const gchar *filename)
{
  if (! filename)
     return NULL;

  gsize bytes_read;
  gsize bytes_written;
  
  return g_filename_to_utf8 (filename, -1, &bytes_read, &bytes_written, NULL);  
}

/*
void make_stats2 (t_note_page *doc)
{
  gboolean selected = TRUE;
  gchar *text;
  gint words = 0;
  gint chars = 0;
  gint white_chars = 0;
  gint lines = 0;
  gint bytes = 0;
  GtkTextIter start;
  GtkTextIter end;
  gunichar ch;

  if (doc_is_sel (doc->text_buffer))
     gtk_text_buffer_get_selection_bounds (doc->text_buffer, &start, &end);
  else
      {
       gtk_text_buffer_get_start_iter (doc->text_buffer, &start); 
       gtk_text_buffer_get_end_iter (doc->text_buffer, &end); 
       selected = FALSE;
      }  

  lines = gtk_text_buffer_get_line_count (doc->text_buffer);
  text =  gtk_text_buffer_get_text (doc->text_buffer, &start, &end, FALSE);
  bytes = strlen (text);

  do 
    {
     ch = gtk_text_iter_get_char (&start);
     if (g_unichar_isspace (ch))
        white_chars++;
     if (gtk_text_iter_starts_word (&start))
        words++; 

     chars++;
    }
  while ( (gtk_text_iter_forward_char (&start)) && ( ! gtk_text_iter_equal (&start, &end)) ); 

  gchar *s_bytes = g_strdup_printf(_("bytes: %d\n"), bytes);
  gchar *s_lines = g_strdup_printf(_("lines: %d\n"), lines);
  gchar *s_words = g_strdup_printf(_("words: %d\n"), words);
  gchar *s_chars = g_strdup_printf(_("chars: %d\n"), chars);
  gchar *s_charsnsp = g_strdup_printf(_("chars non-space: %d\n"), chars - white_chars);

  gchar *result;

  if (! selected)
     result = g_strconcat (_("stats for "),
                           doc->file_name_utf8,
                           ":\n",
                           s_charsnsp,
                           s_chars,
                           s_words,
                           s_lines,
                           s_bytes,
                           NULL);
   else
       result = g_strconcat (_("stats for the selection"),
                             ":\n",
                             s_charsnsp,
                             s_chars,
                             s_words,
                             s_bytes,
                             NULL);

  log_to_memo (result, NULL, LM_NORMAL);

  g_free (s_bytes);
  g_free (s_charsnsp);
  g_free (s_chars);
  g_free (s_words);
  g_free (s_lines);
  g_free (result);
  g_free (text);
}
*/


//from Gedit - plugin
/*
 * docinfo.c
 * This file is part of gedit
 *
 * Copyright (C) 2002 Paolo Maggi

modified by roxton
*/

void make_stats (t_note_page *doc)
{
  if (! doc)
     return;

  if (! doc->text_buffer)
     return;

  gboolean selected = TRUE;

  gchar *text;
  PangoLogAttr *attrs;
  gint words = 0;
  gint chars = 0;
  gint white_chars = 0;
  gint lines = 0;
  gint bytes = 0;
  gint i;
  gchar *tmp_str;

  selected = doc_is_sel (doc->text_buffer);

  if (! selected)
      text = doc_get_buf (doc->text_buffer);
  else
      text = doc_get_sel (doc);

  if (! text)
     return; 

  if (! g_utf8_validate (text, -1, NULL))
     return;

  lines = gtk_text_buffer_get_line_count (doc->text_buffer);

  chars = g_utf8_strlen (text, -1);
  attrs = g_new0 (PangoLogAttr, chars + 1);

  pango_get_log_attrs (text,
                       -1,
                       0,
                       pango_language_from_string ("C"),
                       attrs,
                       chars + 1);

  i = 0;

  while (i < chars)
        {
         if (attrs [i].is_white)
            ++white_chars;

         if (attrs [i].is_word_start)
            ++words;

         ++i;
        }

  if (chars == 0)
     lines = 0;

  bytes = strlen (text);

  gchar *s_bytes = g_strdup_printf(_("bytes: %d\n"), bytes);
  gchar *s_lines = g_strdup_printf(_("lines: %d\n"), lines);
  gchar *s_words = g_strdup_printf(_("words: %d\n"), words);
  gchar *s_chars = g_strdup_printf(_("chars: %d\n"), chars);
  gchar *s_charsnsp = g_strdup_printf(_("chars non-space: %d\n"), chars - white_chars);

  gchar *result;

  if (! selected)
     result = g_strconcat (_("stats for "),
                           doc->file_name_utf8,
                           ":\n",
                           s_charsnsp,
                           s_chars,
                           s_words,
                           s_lines,
                           s_bytes,
                           NULL);
   else
       result = g_strconcat (_("stats for selected:\n"),
                             s_charsnsp,
                             s_chars,
                             s_words,
                             s_bytes,
                             NULL);

  log_to_memo (result, NULL, LM_NORMAL);

  g_free (s_bytes);
  g_free (s_charsnsp);
  g_free (s_chars);
  g_free (s_words);
  g_free (s_lines);
  g_free (result);
  g_free (attrs);
  g_free (text);
}


gchar* get_lang_name (void)
{
  gchar *l = g_getenv ("LANG");
  if (! l)
     return g_strdup ("UTF-8"); 

  if (strcmp (l, "UTF-8") == 0)
     return g_strdup ("UTF-8"); 

  return g_strndup (g_getenv ("LANG"), 2);
}  


gchar* find_good_browser (void)
{
   gchar *t = g_find_program_in_path ("konqueror");
   if (! t)
      t = g_find_program_in_path ("opera");
      if (! t)
         t = g_find_program_in_path ("firefox");
         if (! t)
            t = g_find_program_in_path ("mozilla");
            if (! t)
                t = g_find_program_in_path ("galeon");
                   if (! t)
                      t = g_find_program_in_path ("yelp");

   return t;
}


gchar* compose_browser_cmd (gchar *filename)
{
  if (! filename)
     return NULL;

  gchar *t;
  gchar *b; 

  if (confile.use_def_doc_browser)
     {
      if (confile.cmd_def_doc_browser)
         t = rep_all_s (confile.cmd_def_doc_browser, filename); 
      return t;
     }
  
  b = find_good_browser ();
  
  if (! b)
     return NULL;

  t = g_strconcat (b, " ", filename, " &", NULL);

  g_free (b);
  return t;
}


gchar* get_tea_doc_compose_name (gchar *f)
{
  return g_strconcat (TEA_DOC_DIR, f, NULL);
} 


gchar* find_doc_index_name (void)
{
  gchar *l = get_lang_name ();

  gchar *f = g_strconcat (TEA_DOC_DIR, l, "/index.html", NULL);

  if (! g_file_test (f, G_FILE_TEST_EXISTS))
     {
      g_free (f);
      f = g_strconcat (TEA_DOC_DIR, "en/index.html", NULL);
      g_free (l);
      return f;
     }

  g_free (l);
  return f;
}


void run_doc_in_browser (void)
{
  gchar *f = find_doc_index_name ();
  gchar *t = compose_browser_cmd (f);
  if (t)
     system (t); 

  g_free (f);
  g_free (t);
}


gchar* get_clipboard_text (void)
{
  GtkClipboard *c = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
  if (! c)
     return NULL; 

  if (! gtk_clipboard_wait_is_text_available (c))
     return NULL;

  return gtk_clipboard_wait_for_text (c); 
}  


void insert_image (gchar *a_filename)
{
  if (! get_page_text()) return;
 
  if (! cur_text_doc->b_saved)
     return;

  if (! a_filename)
     return;

  if (! g_file_test (a_filename, G_FILE_TEST_EXISTS))
     return;

  gint width = 0;
  gint height = 0;
  gdk_pixbuf_get_file_info (a_filename, &width, &height);

  gchar *filename = create_relative_link (cur_text_doc->file_name_local, a_filename);


  if (! filename) 
     {
      return;
     }

  gint mm = get_markup_mode ();       
  gchar *t;

  gchar *temp = g_malloc (7777);

  if (mm == MM_HTML) 
      g_snprintf (temp,
                  7777,
                  "<img src=\"%s\" border=\"0\" alt=\"\" width=\"%d\" height=\"%d\">",
                  filename, width, height);
  else
  if (mm == MM_XHTML) 
     g_snprintf (temp,
                 7777,
                 "<img src=\"%s\" border=\"0\" alt=\"\" width=\"%d\" height=\"%d\" />",
                 filename, width, height);
  else
      if (mm == MM_DOCBOOK) 
          g_snprintf (temp,
                      7777,
                      "<mediaobject><imageobject>\n<imagedata fileref=\"%s\"/>\n</imageobject></mediaobject>",
                      filename);
  else
  if (mm == MM_WIKI) 
     g_snprintf (temp,
                 7777,
                 "[[Image:%s|thumb|alternative text]]",
                 filename);
  else
  if (mm == MM_TEX) 
     g_snprintf (temp,
                 7777,
                 "\\includegraphics{%s}",
                 filename);
    
  doc_insert_at_cursor (cur_text_doc, temp);

  g_free (temp);
  g_free (filename);
}


void insert_link (gchar *a_filename)
{
  if (! a_filename)
     return;

  if (! g_file_test (a_filename, G_FILE_TEST_EXISTS))
     return;
  
  gchar *filename = create_relative_link (cur_text_doc->file_name_local, a_filename);
 
  if (! filename) 
     {
      return;
     }
       
  gchar *z =  g_strdup_printf ("<a href=\"%s\"></a>", filename); 

  doc_insert_at_cursor (cur_text_doc, z);
  doc_move_cursor_backw_middle_tags (cur_text_doc);

  g_free (filename);
  g_free (z);
}




void handle_file_ide (gchar *filename, gint line)
{
  if (! filename || ! cur_tea_project)
     return;
 
  gchar *cmd = g_strconcat (cur_tea_project->dir_source, filename, NULL);
 
  gint i = get_n_page_by_filename (cmd);

  if (i != -1)
     {
      gtk_notebook_set_current_page (notebook1, i);
      if (get_page_text ())
          doc_select_line (cur_text_doc, line); 
      return;
     } 

  if (! g_file_test (cmd, G_FILE_TEST_EXISTS))
     return;
  else
      {
       if (! get_page_text() )
          cur_settings.selected_enc = ch_str (cur_settings.selected_enc, "UTF-8");
       else
           cur_settings.selected_enc = ch_str (cur_settings.selected_enc, cur_text_doc->encoding);

       cur_text_doc = open_file_std (cmd);
       doc_select_line (cur_text_doc, line);
      }
}


gboolean parse_error_line (const gchar *line, gchar **filename, gint *lineno)
{
   if (! line)
       return FALSE;

   if (! strstr (line, ":"))
       return FALSE;

   gchar **a = g_strsplit (line, ":", -1);
   
   if (a[0])
      if (a[1])
         if (a[2])
            if (strstr (a[2], "error") || strstr (a[2], "warning")) 
            {
             *filename = g_strstrip (g_strdup (a[0]));
             *lineno = strtol (a[1], NULL, 10);
             g_strfreev (a);   
             return TRUE; 
            }

   g_strfreev (a);   
   return FALSE;
}


void clipboard_put_text (const gchar *s)
{
  GtkClipboard *c = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
  if (c && s)
     gtk_clipboard_set_text (c, s, -1);
}


GList* read_dir_to_glist (gchar *path)
{
  if (! path)
     return NULL;

  GDir *dir = g_dir_open (path, 0, NULL);
  if (! dir) 
     return NULL;

  GList *l = NULL;
  gchar *f = NULL;
  
  while (f = g_dir_read_name (dir))
        l = g_list_prepend (l, g_strconcat (path, G_DIR_SEPARATOR_S, f, NULL));

  g_dir_close (dir);
  return g_list_reverse (l);
}


GList* read_dir_files (gchar *path)
{
  if (! path)
      return NULL;

  GList *l = NULL;

  GDir *d = g_dir_open (path, 0, NULL);  
  gchar *t;

  while (t = g_dir_read_name (d))
        {
         if (! g_file_test (t, G_FILE_TEST_IS_DIR))
             l = g_list_prepend (l, g_strdup (t));
        }

  g_dir_close (d);  
  return g_list_reverse (l);
}

/*
gint get_markup_mode (void)
returns an ID of the markup mode
*/
gint get_markup_mode (void)
{
  if (g_utf8_collate (confile.default_markup_mode, "HTML") == 0)
     return MM_HTML;
  if (g_utf8_collate (confile.default_markup_mode, "XHTML") == 0)
     return MM_XHTML;
  if (g_utf8_collate (confile.default_markup_mode, "Wikipedia") == 0)
     return MM_WIKI;
  if (g_utf8_collate (confile.default_markup_mode, "LaTeX") == 0)
     return MM_TEX;
  if (g_utf8_collate (confile.default_markup_mode, "Docbook") == 0)
     return MM_DOCBOOK;

  return MM_HTML;
} 


gchar* get_amarok_now_playing (void)
{
  gchar *standard_output = NULL;
  gchar *standard_error = NULL;
  gint exit_status;
  GError *error;

  if (g_spawn_command_line_sync ("dcop amarok player nowPlaying", &standard_output,
                                 &standard_error,
                                 &exit_status,
                                 &error))
     g_free (standard_error);

  gchar *x = locale_to_utf8 (standard_output);
  g_free (standard_output);
  return g_strstrip (x);
}


gboolean is_ext (gchar *filename, gchar *ext1, ...)
{
  if (! filename)
     return FALSE;

  va_list args;
  gchar *t;
  gboolean r = FALSE;
  
  va_start (args, ext1);
  t = ext1;
  
  while (t)
        {
         r = check_ext (filename, t);
         if (r)
            break;   

         t = va_arg (args, gchar*);
        }

  va_end (args);

  return r;
}


gchar* run_process_and_wait (gchar *command) 
{
  if (! command)
     return NULL;

  gchar *standard_output = NULL;
  gchar *standard_error = NULL;
  gint exit_status;
  gchar *x = NULL;

  if (command) 
  if (g_spawn_command_line_sync (command, &standard_output,
                                 &standard_error,
                                 &exit_status,
                                 NULL))
     {
      g_free (standard_error);
      x = locale_to_utf8 (standard_output);
      g_free (standard_output);
     }

  return x;
}


void delete_dir (gchar *path)
{
  gchar *cm = g_strconcat ("rm -r -f ", path, NULL);
  system (cm);
  g_free (cm);
}


gchar* correct_relative_filename (const gchar *path, const gchar *filename)
{
  gchar *x = filename_from_xuri (filename); 
  gchar *r;

  if (g_utf8_get_char (x) == '/')
     r = g_strdup (x);
  else
      r = g_strconcat (path, G_DIR_SEPARATOR_S, filename, NULL);

  g_free (x);
  return r;  
} 


gchar* filename_from_xuri (const gchar *uri)
{
  gchar *p = NULL;
  gint mode = 0;

  /*
  0 - no uri
  1 - file:/
  2 - file:///
  */

  p = strstr (uri, "file:///");
  if (p)
     mode = 2;
  else
      {
       p = strstr (uri, "file:/");
       if (p)
          mode = 1;
      }

  if (mode == 0)
     return g_strdup (uri);

  if (mode == 1) 
     p+=5;
  else
      p+=7;
 
  return g_strdup (p);
}


gchar *create_relative_link (const gchar *doc_filename, const gchar *img_filename)
{
  gchar *dir_doc_filename = g_path_get_dirname (doc_filename);
  gchar *dir_img_filename = g_path_get_dirname (img_filename);
  
  gint dir_doc_filename_len = strlen (dir_doc_filename);
  gint dir_img_filename_len = strlen (dir_img_filename);;

  //if the directories are the same, return the pure filename
  
  if (strcmp (dir_doc_filename, dir_img_filename) == 0)
     {
      g_free (dir_doc_filename); 
      g_free (dir_img_filename);
      return g_path_get_basename (img_filename);
     } 

  //if img_filename is in the upper dir than doc_filename, we return the pure filename 
  if (strcmp (dir_img_filename, dir_doc_filename) < 0)
     {
      g_free (dir_doc_filename); 
      g_free (dir_img_filename);
      return g_path_get_basename (img_filename);
     } 

  //if img_filename is in the lower dir than doc_filename, we return the relative filename 
  if (strcmp (dir_doc_filename, dir_img_filename) < 0)
  if (strstr (dir_img_filename, dir_doc_filename))
     {
      
      gchar *rel_path = dir_img_filename + dir_doc_filename_len;      
      gchar *pure_filename = g_path_get_basename (img_filename);
      gchar *x = g_strconcat (".", rel_path, "/", pure_filename, NULL);
      g_free (pure_filename);
      g_free (dir_doc_filename); 
      g_free (dir_img_filename);

      return g_strdup (x);
     }

   return g_path_get_basename (img_filename);
}

///////////////////////WRITE ME
gchar* create_full_path2 (gchar *filename, gchar *basedir) 
{

}
//////////////////////


gboolean is_ext_arr (gchar *filename, gchar *exts)
{
  if (! filename || ! exts)
     return FALSE;
 
  gboolean r = FALSE;

  gchar **arr = g_strsplit (exts, ";", -1);

  if (! arr)
     return FALSE;

  gint c = -1;

  while (arr[c++])
      {
       r = check_ext_wo_dot (filename, arr [c]);
       if (r)
          break;
      }
            
  g_strfreev (arr);

  return r;
}


void add_image_format (GdkPixbufFormat *data, GList **list)
{
  gchar** arr = gdk_pixbuf_format_get_extensions (data); 
  if (! arr)
     return;
 
  gchar *extensions = g_strjoinv (";", arr);
  *list = g_list_prepend (*list, extensions);
  g_strfreev (arr);
}


GList* image_formats_fill (void)
{
  GSList *sl_formats = gdk_pixbuf_get_formats ();
  GList *result = NULL;
  g_slist_foreach (sl_formats, add_image_format, &result);
  g_slist_free (sl_formats);
  return result;
}


gboolean is_image (const gchar *filename)
{
  if (! gl_image_formats)
     return FALSE;

  gboolean r = FALSE;

  GList *t = g_list_first (gl_image_formats);
  while (t)
        {
         r = is_ext_arr (filename, t->data);
         if (r)
             break;           

         t = g_list_next (t);
        }

  return r;
}


gboolean is_writable (gchar *filename)
{
  if (access (filename, W_OK) != 0)
     return FALSE;
  else
      return TRUE;
}


gboolean is_readable (gchar *filename)
{
  if (access (filename, R_OK) != 0)
     return FALSE;
  else
      return TRUE;
}


gboolean has_ext (gchar *filename)
{
  return (strstr (++filename, "."));
}

Generated by  Doxygen 1.6.0   Back to index