[gedit-list] Convert plugin: MAC-DOS-UNIX



Hi!

Ive made a plugin to solve Bug 103753.
http://bugzilla.gnome.org/show_bug.cgi?id=103753

This patch contains the convert/ plugin code.
It guesses the file type {DOS,MAC,UNIX} and uses
gedit_document_replace_string();

It runs a dialog in the middle, for user to abort
conversions,
or convert from     Dos-Unix-Mac-Dos-Unix, 
All the 6 conversion combo's are possible, but based
on
your file type.

You can test the utility by using $unix2dos, dos2unix
tools to make your custom files, and get them
converted via gedit.


Thanks
Muthu.


		
__________________________________ 
Discover Yahoo! 
Stay in touch with email, IM, photo sharing and more. Check it out! 
http://discover.yahoo.com/stayintouch.html

Attachment: convert.tgz
Description: 4044610658-convert.tgz

/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/*
 * convert.c
 * This file is part of gedit
 * [Shamelessly copied from time.c]
 * (C) 2005 Muthiah Annamalai.
 *
 * Copyright (C) 2002 Paolo Maggi 
 *
 * 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.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, 
 * Boston, MA 02111-1307, USA. 
 */
 
/*
 * Modified by the gedit Team, 2002. See the AUTHORS file for a 
 * list of people on the gedit Team.  
 * See the ChangeLog files for a list of changes. 
 */

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

#include <string.h>
#include <time.h>

#include <glib/gi18n.h>
#include <glade/glade-xml.h>
#include <gconf/gconf-client.h>
#include <libgnome/gnome-help.h>
#include <libgnome/gnome-config.h>

#include <gedit/gedit-plugin.h>
#include <gedit/gedit-debug.h>
#include <gedit/gedit-menus.h>
#include <gedit/gedit-utils.h>
#include <gedit/gedit-document.h>


#define MENU_ITEM_LABEL		N_("Convert _Unix &lt;-&gt; Dos &lt;-&gt; Mac &lt;-&gt; Unix")
#define MENU_ITEM_PATH		"/menu/Edit/EditOps_3/"
#define MENU_ITEM_NAME		"PluginConvertEncoding"	
#define MENU_ITEM_TIP		N_("Convert OS formats between Mac, Unix &amp; Dos")

/* For the people to pickup.. */
G_MODULE_EXPORT GeditPluginState update_ui (GeditPlugin *plugin, BonoboWindow *window);
G_MODULE_EXPORT GeditPluginState destroy (GeditPlugin *pd);
G_MODULE_EXPORT GeditPluginState activate (GeditPlugin *pd);
G_MODULE_EXPORT GeditPluginState deactivate (GeditPlugin *pd);
G_MODULE_EXPORT GeditPluginState init (GeditPlugin *pd);
G_MODULE_EXPORT GeditPluginState configure (GeditPlugin *p, GtkWidget *parent);

/* Painfully UniThreaded....! */
static void 
convert_os (BonoboUIComponent *uic,gpointer user_data, const gchar* verbname)
{	
	GeditDocument *doc;
	GtkTextIter sit={0,},eit={0,};
	GtkWidget *dlg=NULL;
	GtkWidget *pbar=NULL,*ebox=NULL;
	gchar *data=NULL;
	gchar *add_str=NULL;
	gchar *ip_str=NULL;

	int length=0,query_length=0,i=0;
	int mac_count=0,dos_count=0,unix_count=0;


	enum {IN=0,OUT=1};
	enum {DOS=0, MAC=1, UNIX=2} FileType[2];
	char *FileTypeString[]={"DOS \\r\\n CR+LF","MAC \\r CR","UNIX \\n LF"};
	GtkWidget *FileTypeWidget[2]={0,};
	GtkTooltips *ttips=NULL;
	GString *opstr=NULL;

	gedit_debug (DEBUG_PLUGINS, "Conversion Routine.");
	
	/* For Undo Redo stuff? */
	doc = gedit_get_active_document ();
	gedit_document_begin_user_action (doc);


	gedit_debug (DEBUG_PLUGINS, "Conversion Routine 2");

	gtk_text_buffer_get_start_iter(GTK_TEXT_BUFFER (doc),&sit);
	gtk_text_buffer_get_end_iter(GTK_TEXT_BUFFER (doc),&eit);

	gedit_debug (DEBUG_PLUGINS, "Conversion Routine 3");
	/* Can be very slow.... */
	data=gtk_text_buffer_get_text(GTK_TEXT_BUFFER (doc),
				      &sit,&eit,FALSE);
	g_return_if_fail(data!=NULL || strlen(data)<1);

	gedit_debug (DEBUG_PLUGINS, "Conversion Routine 4");
	length=strlen(data);
	
	/* MIN 200 characters or {FILE_LENG} to probe the type of file*/
	if(length>200)
	{
		query_length=200;
	}
	else
		query_length=length;

	/* Probing. */
	for(i=0;i<(query_length-1);i++)
	{
		if(data[i]=='\r' && data[i+1]=='\n')
			dos_count++;
		else if(data[i]=='\r')
			mac_count++;
		else if(data[i]=='\n')
			unix_count++;
	}
	
	FileType[IN] = (mac_count > unix_count) ? MAC : UNIX;	
	FileType[IN] = (MAX(mac_count,unix_count) > dos_count) ? FileType[IN] : DOS;

	/* Popup-dialog: Buildit earlier in Init or something.*/
	dlg=gtk_message_dialog_new(NULL,0,
				   GTK_MESSAGE_QUESTION,
				   GTK_BUTTONS_NONE,
				   "This file type is %s.\n Choose the Format you want to convert this file to.",FileTypeString[FileType[IN]]);


	switch(FileType[IN])
	{
	case DOS:
		FileTypeWidget[0]=gtk_button_new_with_label("Dos -> Unix");
		ttips=gtk_tooltips_new();
		gtk_tooltips_set_tip(ttips,FileTypeWidget[0],
				     "Convert \\r\\n to \\n in Text","");

		FileTypeWidget[1]=gtk_button_new_with_label("Dos -> Mac");
		ttips=gtk_tooltips_new();
		gtk_tooltips_set_tip(ttips,FileTypeWidget[1],
				     "Convert \\r\\n to \\r in Text","");


		break;
	case MAC:
		FileTypeWidget[0]=gtk_button_new_with_label("Mac -> Dos");
		ttips=gtk_tooltips_new();
		gtk_tooltips_set_tip(ttips,FileTypeWidget[0],
				     "Convert \\r to \\r\\n in Text","");
		FileTypeWidget[1]=gtk_button_new_with_label("Mac -> Unix");
		ttips=gtk_tooltips_new();
		gtk_tooltips_set_tip(ttips,FileTypeWidget[1],
				     "Convert \\r to \\n in Text","");


		break;
 	case UNIX:
		FileTypeWidget[0]=gtk_button_new_with_label("Unix -> Dos");
		ttips=gtk_tooltips_new();
		gtk_tooltips_set_tip(ttips,FileTypeWidget[0],
				     "Convert \\n to \\r\\n in Text","");

		FileTypeWidget[1]=gtk_button_new_with_label("Unix -> Mac");
		ttips=gtk_tooltips_new();
		gtk_tooltips_set_tip(ttips,FileTypeWidget[1],
				     "Convert \\n to \\r in Text","");
		break;
	}
	FileTypeWidget[2]=gtk_button_new_from_stock(GTK_STOCK_CANCEL);


 	gtk_dialog_add_action_widget(GTK_DIALOG(dlg),
				     FileTypeWidget[0],
				     0);
	
	gtk_dialog_add_action_widget(GTK_DIALOG(dlg),
				     FileTypeWidget[1],
				     1);

	gtk_dialog_add_action_widget(GTK_DIALOG(dlg),
				     FileTypeWidget[2],
				     2);
				     
	gtk_widget_show_all(dlg);
	
	gedit_debug(DEBUG_PLUGINS,"Dialog Runs...");

	/* Modal dialog */
	FileType[OUT]=gtk_dialog_run(GTK_DIALOG(dlg));	
	gedit_debug(DEBUG_PLUGINS,"Dialog Finished.. %d",FileType[OUT]);

	/* MAGIC! */
	while(gtk_events_pending())
		gtk_main_iteration();

	gtk_widget_hide(dlg);
	gtk_widget_destroy(dlg);

	/* MAGIC! */
	while(gtk_events_pending())
		gtk_main_iteration();


	gedit_debug(DEBUG_PLUGINS,"Dialog Destroyed.. %d",FileType[OUT]);



	if(FileType[OUT]==2)
	{
		gedit_debug(DEBUG_PLUGINS,"User aborted Convert operation");
		return;
	}

	opstr=g_string_new(NULL);

	gedit_debug(DEBUG_PLUGINS,"Progression Action");
	gedit_debug(DEBUG_PLUGINS,"ft[IN]=%d ft[OUT]=%d",
		    FileType[IN],FileType[OUT]);

	g_assert(data!=NULL);
	gedit_debug(DEBUG_PLUGINS,"Iter %d",i);
	
	add_str=NULL;
	ip_str=NULL;
	switch(FileType[IN])
	{
	case DOS:
		gedit_debug(DEBUG_PLUGINS,FileTypeString[FileType[OUT]+1]);
		ip_str="\r\n";
		/* MAC */
		if(FileType[OUT]==0)
			add_str="\r";
		/* UNIX */
		else if(FileType[OUT]==1)
			add_str="\n";
		
		g_assert(add_str!=NULL);
		opstr=g_string_append(opstr,add_str);				
		break;
	case MAC:
		gedit_debug(DEBUG_PLUGINS,FileTypeString[(OUT==1)?2:0]);
		ip_str="\r";
		
		/* DOS */
		if(FileType[OUT]==0)
			add_str="\r\n";
		/* UNIX */
		else if(FileType[OUT]==1)
			add_str="\n";
				
		g_assert(add_str!=NULL);
		opstr=g_string_append(opstr,add_str);
		break;
	case UNIX:
		gedit_debug(DEBUG_PLUGINS,FileTypeString[FileType[OUT]]);
		ip_str="\n";
			
		/* DOS */
		if(FileType[OUT]==0)				
			add_str="\r\n";

		/* MAC */
		else if(FileType[OUT]==1)
			add_str="\r";
		
		g_assert(add_str!=NULL);
		opstr=g_string_append(opstr,add_str);
		break;
	default:
		g_message("Critical region IN=%d",IN);
		return;
	}

	/* MAGIC! */
	while(gtk_events_pending())
		gtk_main_iteration();


	gtk_text_buffer_get_start_iter(GTK_TEXT_BUFFER(doc),&sit);
	gtk_text_buffer_place_cursor (GTK_TEXT_BUFFER(doc),&sit);

	g_assert(opstr->str!=NULL);
	gedit_document_replace_all(doc,ip_str /*Find */,
				   opstr->str /* Replace */,
				   GEDIT_SEARCH_FROM_CURSOR);
				   

	g_string_free(opstr,TRUE);
	if(data)
		g_free(data);
	/* DONE! */

	gedit_document_end_user_action (doc);

	return;
}

G_MODULE_EXPORT GeditPluginState
configure (GeditPlugin *p, GtkWidget *parent)
{
	GtkWidget *dlg=NULL;
	gedit_debug (DEBUG_PLUGINS, "Informing Convert Dialog");	
	dlg=gtk_message_dialog_new
		(NULL,GTK_DIALOG_MODAL,GTK_MESSAGE_INFO,GTK_BUTTONS_OK,
		 "To Convert text from MAC<to>DOS<to>UNIX<to>MAC fromats");
	g_return_val_if_fail(dlg!=NULL,PLUGIN_OK);

	gtk_widget_show_all(dlg);
	gtk_dialog_run(GTK_DIALOG(dlg));
	gtk_widget_destroy (dlg);

	gedit_debug (DEBUG_PLUGINS, "Done");

	return PLUGIN_OK;
}

G_MODULE_EXPORT GeditPluginState
update_ui (GeditPlugin *plugin, BonoboWindow *window)
{
	BonoboUIComponent *uic;
	GeditDocument *doc;
	GeditMDI *mdi;

	gedit_debug (DEBUG_PLUGINS, "OS Update ui");
	
	g_return_val_if_fail (window != NULL, PLUGIN_ERROR);

	mdi = gedit_get_mdi ();
	g_return_val_if_fail (window != NULL, PLUGIN_ERROR);

	uic = gedit_get_ui_component_from_window (window);

	doc = gedit_get_active_document ();

	if ((doc == NULL) || (gedit_document_is_readonly (doc)) || (gedit_mdi_get_state (mdi) != GEDIT_STATE_NORMAL))		
		gedit_menus_set_verb_sensitive (uic, "/commands/" MENU_ITEM_NAME, FALSE);
	else
		gedit_menus_set_verb_sensitive (uic, "/commands/" MENU_ITEM_NAME, TRUE);

	return PLUGIN_OK;
}
	
G_MODULE_EXPORT GeditPluginState
activate (GeditPlugin *pd)
{
	 /* Standard stuff */
	GList *top_windows;
        gedit_debug (DEBUG_PLUGINS, "");

        top_windows = gedit_get_top_windows ();
        g_return_val_if_fail (top_windows != NULL, PLUGIN_ERROR);

        while (top_windows)
        {
		gedit_menus_add_menu_item (BONOBO_WINDOW (top_windows->data),
				     MENU_ITEM_PATH, MENU_ITEM_NAME,
				     MENU_ITEM_LABEL, MENU_ITEM_TIP,
				     NULL, convert_os);

                pd->update_ui (pd, BONOBO_WINDOW (top_windows->data));

                top_windows = g_list_next (top_windows);
        }
       
	
	return PLUGIN_OK;
}

G_MODULE_EXPORT GeditPluginState
deactivate (GeditPlugin *pd)
{
	gedit_debug (DEBUG_PLUGINS, "Deactivated Convert  plugin");

	gedit_menus_remove_menu_item_all (MENU_ITEM_PATH, MENU_ITEM_NAME);

	return PLUGIN_OK;
}

G_MODULE_EXPORT GeditPluginState
destroy (GeditPlugin *pd)
{
	gedit_debug (DEBUG_PLUGINS, "Closing Convert Plugin");
	return PLUGIN_OK;
}

G_MODULE_EXPORT GeditPluginState
init (GeditPlugin *pd)
{
	/* initialize */
	gedit_debug (DEBUG_PLUGINS, "Convert Init plugin");

	pd->private_data = NULL;
		
	/* NO GConf Stuff */	
	return PLUGIN_OK;
}



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]