GTK+ Forums Forum Index GTK+ Forums
Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Clipboard Monitor (for download managers) (C)

 
Post new topic   Reply to topic    GTK+ Forums Forum Index -> GTK+ Example Code
Author Message
vampirefrog



Joined: 12 Aug 2009
Posts: 3

PostPosted: Sat Dec 26, 2009 4:31 pm    Post subject: Clipboard Monitor (for download managers) (C) Reply with quote

Actually it's C++ but you can easily convert it to C.

It basically works by getting the clipboard contents every 250ms, and comparing it to the previous value. If it has changed, it tries to look for http:// links (you can add https and ftp too). The code might be bugged, it's just a quick working hack. I strongly suggest changing the url detection code. There might be better ways to implement it too. The idea was taken from d4x 2.5.7.2 code (see main/face/list.cc lines 1876, 1897, 1932, 2089).

Code: (Plaintext)
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

#include <gtk/gtk.h>
#include <string.h>

void on_window_destroy(GtkObject * object, gpointer user_data) {
    gtk_main_quit();
}

char *old_clipboard = NULL;
void selection_received(GtkWidget *window, GdkEvent *event) {
    char *buf;
    GdkAtom atom;
    gint format, length;

    length = gdk_selection_property_get(window->window, (guchar **)&buf, &atom, &format);
    if(old_clipboard && !strcmp(old_clipboard, buf)) {
        g_free(buf);
    } else {
        char *b = buf;
        char *c;
        while(b && (c = strstr(b, "http://"))) {
            char *p = strpbrk(c, " \t\r\n");
            char *url = p ? g_strndup(c, p - c) : g_strdup(c);
            puts(url); // handle url
            g_free(url);
            b = c + 1;
        }
        old_clipboard = buf;
    }
}

int clipboard_timeout(void *window) {
    gtk_selection_convert(GTK_WIDGET(window), GDK_SELECTION_CLIPBOARD, GDK_TARGET_STRING, GDK_CURRENT_TIME);
}

int main(int argc, char *argv[]) {
    GtkBuilder *builder;

    GtkWidget *window;

    gtk_init(&argc, &argv);

    builder = gtk_builder_new();
    gtk_builder_add_from_file(builder, "cm.ui", NULL);

    window = GTK_WIDGET(gtk_builder_get_object(builder, "window1"));
    gtk_builder_connect_signals(builder, NULL);
    g_object_unref(G_OBJECT(builder));

    gtk_widget_show(window);

    // Monitor clipboard
    g_signal_connect(G_OBJECT(window), "selection-received", G_CALLBACK(selection_received), NULL);
    g_timeout_add(250, clipboard_timeout, window);

    gtk_main();

    return 0;
}


And here's the simple UI file for it, cm.ui. It's basically unused, except for the main window, window1:
Code: (XML/HTML)
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

<?xml version="1.0"?>
<interface>
  <requires
lib="gtk+" version="2.16"/>
 
<!-- interface-naming-policy project-wide -->
 
<object class="GtkWindow" id="window1">
    <property
name="border_width">4</property>
    <signal
name="delete_event" handler="gtk_main_quit"/>
    <child>
      <object
class="GtkVBox" id="vbox1">
        <property
name="visible">True</property>
        <property
name="orientation">vertical</property>
        <child>
          <placeholder/>
        </child>
        <child>
          <object
class="GtkImage" id="image1">
            <property
name="visible">True</property>
            <property
name="stock">gtk-yes</property>
          </object>
          <packing>
            <property
name="position">1</property>
          </packing>
        </child>
        <child>
          <object
class="GtkButton" id="button1">
            <property
name="label" translatable="yes">button</property>
            <property
name="visible">True</property>
            <property
name="can_focus">True</property>
            <property
name="receives_default">True</property>
          </object>
          <packing>
            <property
name="position">2</property>
          </packing>
        </child>
      </object>
    </child>
  </object>
</interface>
Back to top
Display posts from previous:   
Post new topic   Reply to topic    GTK+ Forums Forum Index -> GTK+ Example Code All times are GMT
Page 1 of 1

 


Powered by phpBB © 2001, 2005 phpBB Group
CodeBB 1.0 Beta 2
Protected by Anti-Spam ACP