Ejemplo n.º 1
0
/* Method: mime_types
 * Returns: a list of mime types for the given language, as an array of strings.
 */
static VALUE
rg_mime_types(VALUE self)
{
    VALUE ary;
    char **types = gtk_source_language_get_mime_types (_SELF (self));
    if (!types)
        return Qnil;

    ary = rb_ary_new();
    while (*types){
        rb_ary_push(ary, CSTR2RVAL(*types));
        types++;
    }
    return ary;
}
Ejemplo n.º 2
0
GtkSourceLanguage *
gitg_utils_get_language(gchar const *content_type)
{
	if (!gitg_utils_can_display_content_type(content_type))
		return NULL;
	
	gchar *mime = g_content_type_get_mime_type(content_type);
	GtkSourceLanguageManager *manager = gtk_source_language_manager_get_default();
	
	gchar const * const *ids = gtk_source_language_manager_get_language_ids(manager);
	gchar const *ptr;
	GtkSourceLanguage *ret;
	
	while ((ptr = *ids++))
	{
		ret = gtk_source_language_manager_get_language(manager, ptr);
		gchar **mime_types = gtk_source_language_get_mime_types(ret);
		gchar **types = mime_types;
		gchar *m;
		
		if (types)
		{
			while ((m = *types++))
			{
				if (strcmp(mime, m) == 0)
				{
					g_free(mime);
					g_strfreev(mime_types);
					return ret;
				}
			}
		
			g_strfreev(mime_types);
		}

		ret = NULL;
	}
	
	g_free(mime);
	return NULL;
}
Ejemplo n.º 3
0
void save_as_file(GtkButton *button) {
  /** Save the current editor content as the choosen file. **/

  #ifdef DEBUG
    DEBUG_FUNC_MARK
  #endif





  GtkWidget *file_chooser = gtk_file_chooser_dialog_new( _("Save as file"),
                                                        GTK_WINDOW(gui->main_window),
                                                        GTK_FILE_CHOOSER_ACTION_SAVE,
                                                        _("Cancel"),  GTK_RESPONSE_CANCEL,
                                                        _("Save as"), GTK_RESPONSE_ACCEPT,
                                                        NULL) ;


  /** Retrieve the stored filepath: **/
  gpointer stored_filepath = g_object_get_data(G_OBJECT(current_editor.current_buffer), "filepath") ;

  /** Storing last opened file folder. **/
  if (open_file_dirname != NULL) {
    g_free(open_file_dirname) ;
  }
  open_file_dirname = g_strdup(g_path_get_dirname(stored_filepath)) ;

  gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(file_chooser), open_file_dirname );

  gint res;

  GtkFileChooser *chooser;
  chooser = GTK_FILE_CHOOSER(file_chooser);


  gtk_file_chooser_set_do_overwrite_confirmation(chooser, TRUE);

  res = gtk_dialog_run(GTK_DIALOG(file_chooser)) ;

  if (res == GTK_RESPONSE_ACCEPT) {

    char *filepath ;
    filepath = gtk_file_chooser_get_filename(chooser);

    /** Getting current editor content **/
    GtkTextIter iter_start, iter_end  ;
    GError *error = NULL              ;

    gtk_text_buffer_get_start_iter(current_editor.current_buffer, &iter_start);
    gtk_text_buffer_get_end_iter(current_editor.current_buffer,   &iter_end);

    gchar *file_content = gtk_text_buffer_get_text(current_editor.current_buffer, &iter_start, &iter_end, FALSE) ;


    if (! g_file_set_contents(filepath, file_content, -1, &error) ) {
      /** Failed to save editor content as file, display an error message and return. **/

      char *msg = g_strdup_printf(_("Failed to save file:\n%s"), filepath) ;

      display_message_dialog(_("Cannot save file !!!"), msg, GTK_MESSAGE_ERROR, GTK_BUTTONS_CLOSE) ;

      free(msg) ;

      return ;

    }

    /** Mark the TextBuffer as not modfied. **/
    gtk_text_buffer_set_modified(current_editor.current_buffer, FALSE) ;

    /** Only useful if the content type has changed like a new file saved as a *.c file. **/
    GtkSourceLanguage        *source_language         = NULL ;
    GtkSourceLanguageManager *source_language_manager = gtk_source_language_manager_get_default();

    gboolean result_uncertain ;
    gchar *content_type ;

    content_type = g_content_type_guess( g_path_get_basename(filepath), (const guchar *) file_content, strlen(file_content), &result_uncertain) ;

    if (content_type && source_language_manager) {

      source_language = gtk_source_language_manager_guess_language(source_language_manager, g_path_get_basename(filepath), content_type);

      if (source_language) {

        set_syntax_highlight_radio(gtk_source_language_get_id(source_language)) ;

        gtk_source_buffer_set_language(GTK_SOURCE_BUFFER(current_editor.current_buffer), source_language) ;

        g_object_set_data(G_OBJECT(current_editor.current_textview), "lang_id", (char *) gtk_source_language_get_id(source_language)) ;
      }

      g_free(content_type) ;
    }



    /** Update the notebook label tab **/
    GtkWidget *notebook_tab = gtk_notebook_get_tab_label(GTK_NOTEBOOK(gui->editor_notebook), current_editor.current_notebook_page);

    /** The tab contains an mimetype icon, the filename and the page closing button. **/
    GList *tab_compound_list = gtk_container_get_children(GTK_CONTAINER(notebook_tab)) ;

    tab_compound_list = g_list_first(tab_compound_list) ;

    while (tab_compound_list->data != NULL) {
        /** We iterate over the notebook tab component **/
        if  (g_object_get_data(G_OBJECT(tab_compound_list->data), "tab_filename_widget")) {
          /** Set the new filename in the tab. **/
          gtk_label_set_text(GTK_LABEL(tab_compound_list->data), g_path_get_basename(filepath)) ;
        }

        if  (g_object_get_data(G_OBJECT(tab_compound_list->data), "tab_icon") && source_language) {


          uint8_t c ;
          for ( c=0 ; ; c++) {
    
              if (gtk_source_language_get_mime_types(source_language) == NULL) {
     
                break ;
              }
    
              char *mimetype = gtk_source_language_get_mime_types(source_language)[c] ;

              if (mimetype == NULL) {
                  /** don't find an specific mimetype for this new file extension use default icon. **/
                  g_object_set(G_OBJECT(tab_compound_list->data),"file", PATH_TO_MIMETYPE_ICON "unknown.png", NULL) ;
                  break ;
              }

              /** We search for an image filename ending with the corresponding mimetype: **/
              char *ptr = strchr(mimetype, '/') ;

              if (ptr != NULL) {

                /** Simply pointer arithmetic to exchange the '/' (used in mimetypes) and the '-' (used in the images names) character **/
                mimetype[ptr - mimetype] = '-' ;


                gchar *filepath = g_strdup_printf("%s%s.png", PATH_TO_MIMETYPE_ICON, mimetype) ;

                if ( g_file_test(filepath, G_FILE_TEST_EXISTS) ) {
                  /** We found an corresponding image for this mimetype. **/
                  g_object_set(G_OBJECT(tab_compound_list->data),"file", filepath, NULL) ;
                  free(filepath) ;
                  break ;
                }

                free(filepath) ;

              }

          }
        }

        if (tab_compound_list->next == NULL) {
          break ;
        }

        tab_compound_list = tab_compound_list->next ;
    }

    g_free(file_content)   ;

    /** Storing filepath for further saving operations. **/
    g_object_set_data(G_OBJECT(current_editor.current_buffer), "filepath", filepath) ;

    /** setting the base filename in the bottom bar. **/
    gtk_label_set_text(GTK_LABEL(gui->bottom_bar->filename_label), g_path_get_basename(filepath)) ;


    if (settings.rm_trailing_spaces) {
      /** Deleting trailing spaces. **/
      char *trailing_spaces_deleting ;
      trailing_spaces_deleting = g_strdup_printf("sed -i 's/[[:space:]]$//' '%s'", (char *) filepath) ;
      int ret ;
      if ((ret = system(trailing_spaces_deleting)) == -1) {
        g_warning( _("Removing trailing space failure:\n%s\n"), trailing_spaces_deleting) ;
      }

      free(trailing_spaces_deleting) ;
    }
   
    File_Editor *file_editor = (File_Editor *) g_object_get_data(G_OBJECT(current_editor.current_textview), "file_editor") ;
   
    gtk_notebook_set_menu_label_text(GTK_NOTEBOOK(gui->editor_notebook), file_editor->scrolled_window, g_path_get_basename(filepath) ) ;
   
    #ifdef RELOADING_FUNC
    /** Update Last modification timestamp. **/
    File_Editor *file_editor = (File_Editor *) g_object_get_data(G_OBJECT(current_editor.current_textview), "file_editor") ;
    g_stat(filepath, &file_editor->file_info) ;
    #endif
   
   

  }

  /** @NOTE: the filepath must not be free because it is set as data from the file_editor->buffer for further use. **/

  gtk_widget_destroy(file_chooser);
}