Work at SourceForge, help us to make it a better place! We have an immediate need for a Support Technician in our San Francisco or Denver office.

Close

[b0bd45]: notification-tail.c Maximize Restore History

Download this file

notification-tail.c    154 lines (127 with data), 4.1 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <gtk/gtk.h>
#include <sys/inotify.h>
#include "notification.h"
static gchar **files;
static GtkWidget *window=NULL;
static int tail_size=1024;
static GOptionEntry entries[] =
{
{ "size", 's', 0, G_OPTION_ARG_INT, &tail_size,"The size in bytes of the tail part that is to be read",NULL},
{ G_OPTION_REMAINING,0,0,G_OPTION_ARG_FILENAME_ARRAY,&files,"File to be watched", "TEXT_FILE" },
{ NULL }
};
GtkWidget *text_view_new_from_file(const gchar *file_path)
{
long pos;
char *buffer,*head;
FILE *file;
GtkWidget *text_view;
GtkTextBuffer *text_buffer;
head=NULL;
file=fopen(file_path,"r");
buffer = (char *) calloc ((tail_size+1),sizeof (char));
file = fopen (file_path, "r");
fseek(file,-tail_size,SEEK_END);
pos=ftell(file);
fread (buffer, tail_size, 1, file);
fclose(file);
if (pos>0)
head=strsep(&buffer,"\n"); // remove the top 'messed up' line
text_view = gtk_text_view_new();
text_buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (text_view));
gtk_text_buffer_set_text(text_buffer, buffer,strlen(buffer));
gtk_text_view_set_editable(GTK_TEXT_VIEW(text_view),FALSE);
if (head)
free(head);
else
free(buffer);
return text_view;
}
void interface_destroy(void)
{
if (window)
{
gtk_widget_hide(window);
gtk_widget_destroy(window);
window=NULL;
}
}
static gboolean window_delete_event(GtkWidget *widget, GdkEvent *event, gpointer user_data)
{
notification_interface_quit();
return FALSE;
}
static void icon_handle_activate(GtkStatusIcon *status_icon, gpointer gp)
{
if (!window)
{
GtkWidget *scrolled_window;
GtkWidget *text_view;
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
scrolled_window=gtk_scrolled_window_new(NULL,NULL);
gtk_window_set_title(GTK_WINDOW(window), notification_file_name());
gtk_container_add(GTK_CONTAINER (window), scrolled_window);
text_view = text_view_new_from_file(notification_file_path());
gtk_container_add(GTK_CONTAINER (scrolled_window), text_view);
/* Exit when the window is closed */
g_signal_connect(window,"delete-event", G_CALLBACK (window_delete_event),NULL);
//g_signal_connect(window, "destroy", G_CALLBACK (window_destroy), NULL);
//g_signal_connect(window, "destroy-event", G_CALLBACK (window_destroy_event), NULL);
}
if (window)
{
if (gtk_widget_get_visible(window)) //data->visible)
{
gtk_widget_hide(window);
//data->visible=FALSE;
}
else
{
/*
GdkVisual *visual;
GdkScreen *screen;
screen = gtk_widget_get_screen(data->window);
visual = gdk_screen_get_rgba_visual(screen);
printf("screen %x visual %x\n",(unsigned int)screen,(unsigned int)visual);
if (visual == NULL)
visual = gdk_screen_get_system_visual(screen);
gtk_widget_set_visual(data->window, visual);
printf("composit? %d\n",gtk_widget_is_composited(data->window));
//gdk_window_set_composited(data->window,TRUE);
*/
gtk_widget_show_all(window);
gtk_window_present(GTK_WINDOW(window));
}
}
}
int main(int argc, char **argv)
{
GError *error = NULL;
GOptionContext *context;
context = g_option_context_new ("- create a notification on tab seperated file write close");
g_option_context_add_main_entries (context, entries, NULL);
g_option_context_add_group(context,notification_get_option_group());
g_option_context_add_group(context, gtk_get_option_group (TRUE));
if (!g_option_context_parse (context, &argc, &argv, &error))
{
g_print ("option parsing failed: %s\n", error->message);
g_error_free(error);
exit (1);
}
notification_create(files[0],
G_CALLBACK(icon_handle_activate),
&interface_destroy,IN_MODIFY);
g_strfreev(files);
notification_main();
g_option_context_free(context);
notification_destroy();
if (window)
{
g_object_unref(window);
gtk_widget_destroy(window);
}
return 0;
}