Hacking Tomboy

De Wikijoan
Dreceres ràpides: navegació, cerca

Contingut

Què és Tomboy

El Tomboy és una eina per prendre notes, que ve instal.lada per defecte en el Ubuntu. Es basa en fitxers XML, per tant en teoria hauria de ser fàcil importar i exportar les notes des del Tomboy i cap al Tomboy, des d'altres aplicacions. Estic pensant, concretament en importar/exportar al sistema de notes del Android.

Tots els fitxers de configuració estan a /home/joan/.tomboy, i veiem que les notes tenen format XML. Per exemple, la nota de33e232-7382-4518-a9b9-70d3e388b81f.note:

<?xml version="1.0" encoding="utf-8"?>
<note version="0.3" xmlns:link="http://beatniksoftware.com/tomboy/link" xmlns:size="http://beatniksoftware.com/tomboy/size" xmlns="http://beatniksoftware.com/tomboy">
  <title>Utilitzando enlaces en Tomboy</title>
  <text xml:space="preserve"><note-content version="0.1">Utilitzando enlaces en Tomboy

Las notas en Tomboy se pueden enlazar subrayando el texto en la nota actual y pulsando el botón <bold>Enlazar</bold> en la barra de herramientas. Al hacerlo se creará una nueva nota así como subrayar el título de la nueva nota en la nota actual.

Cambiar el título de una nota hará que se actualicen los enlaces presentes en otras notas. Esto previene que aparezcan enlaces rotos cuando se renombra una nota.

Además, si escribe el nombre de otra nota en su nota actual, se enlazará automáticamente.</note-content></text>
  <last-change-date>2009-04-24T10:05:51.2512770+02:00</last-change-date>
  <last-metadata-change-date>2009-04-24T10:05:51.2512770+02:00</last-metadata-change-date>
  <create-date>2009-04-24T09:25:27.0347990+02:00</create-date>
  <cursor-position>6</cursor-position>
  <width>450</width>
  <height>360</height>
  <x>716</x>
  <y>386</y>
  <open-on-startup>False</open-on-startup>
</note>

Volia fer un hacking senzill i editar manualment aquest fitxer i veure com quedava reflexat el canvi... però no, no és tan senzill. Sembla ser que hi ha una base de dades pel mig, en els directoris addin-db-001, que inclou els subdirectoris addin-data i addin-dir-data, i suposo que aquí es guarda la informació. Fins que no entengui ben bé el procés de com es guarden les notes, no es podrà fer hacking.

El forum de desenvolupament de Tomboy està a: http://osdir.com/ml/gnome.tomboy/

Com crear un plugin

http://live.gnome.org/Tomboy/HowToCreateAddins

In this HowTo we will develop a simple plugin for Tomboy using C#. The plugin will insert the current date and time at the cursor position, much like the Insert Date/Time plugin in gedit.

The plugin file

Let's start with creating the plugin file called InsertDateTime.cs with the following content (els fitxers els creo dins de $HOME/.tomboy/addins):

using Tomboy;

namespace Tomboy.InsertDateTime
{
        public class InsertDateTimeAddin : NoteAddin
        {
                public override void Initialize ()
                {
                }

                public override void Shutdown ()
                {
                }

                public override void OnNoteOpened ()
                {
                }
        }
}

The first thing you should notice is that our plugin extends the NoteAddin class. The name NoteAddin reveals how plugins are handled in Tomboy: an instance of the plugin is created per note. From within the plugin you have access to the associated note, its window and other stuff useful for a plugin.

A plugin must implement three abstract methods: Initialize, Shutdown and OnNoteOpened. Initialize is called at startup, when the plugin instances are created for each note, or when the plugin is activated in the preferences dialog. Respectively Shutdown is called when the plugin is deactivated. OnNoteOpened will be executed when the note window is opened for the first time.

The metadata file

We also need to create a metadata XML-file called InsertDateTime.addin.xml with the following content:

<Addin id="DateTimeAddin"
    namespace="Tomboy"
    name="Insert Date/Time"
    author="Tomboy Project"
    description="Inserts current date and time at the cursor position."
    category="Tools"
    defaultEnabled="false"
    version="0.1">

    <Runtime>
        <Import assembly="InsertDateTime.dll" />
    </Runtime>

    <Dependencies>
        <Addin id="Tomboy" version="0.7" />
    </Dependencies>

    <Extension path="/Tomboy/NoteAddins">
        <NoteAddin type="Tomboy.InsertDateTime.InsertDateTimeAddin" />
    </Extension>
</Addin>

This information is displayed on Tomboy's preferences dialog, and is what makes the addin system understand that this is a plugin.

Adding the menu item

Això no sé on ficar-ho, de moment m'ho salto

using System;
using Mono.Unix;
using Gtk;
using Tomboy;

namespace Tomboy.InsertDateTime
{
        public class InsertDateTimeAddin : NoteAddin
        {
                Gtk.MenuItem item;

                public override void Initialize ()
                {
                        item = new Gtk.MenuItem (Catalog.GetString ("Insert date and time"));
                        item.Activated += OnMenuItemActivated;
                        item.Show ();
                        AddPluginMenuItem (item);
                }

                public override void Shutdown ()
                {
                        item.Activated -= OnMenuItemActivated;
                }

                public override void OnNoteOpened ()
                {
                }

                void OnMenuItemActivated (object sender, EventArgs args)
                {
                        Logger.Log ("Activated 'Insert date and time' menu item");
                }
        }
}

For inserting the date, our plugin will need an item in the menu. We create the entry in Initialize and store it in an instance variable for later usage. It should be possible to translate its text, that's why we get it from Catalog. The item is added to the menu via the helper method AddPluginMenuItem which is defined in the plugin's base class NoteAddin. When the item is activated, the callback method OnMenuItemActivated is called, which only writes a log message at the moment (Tomboy has its own logger class and does not use Console.WriteLine). The Shutdown method should do the clean up work, in our case, we have to remove the registered callback.

Sometimes it is necessary to do some initialisation work only once and not for every note. An example how to do that can be found in Tomboy's NoteOfTheDay plugin.

Inserting the date

Això no sé on ficar-ho, de moment m'ho salto

Our last step is inserting the date at the cursor position. To achive this, we can use some stuff already used in Tomboy: the date format and the tag that defines the date's appearance. We get the buffer, which is a Gtk.TextBuffer, from the note that belongs to the plugin and insert the formatted date using the datetime tag.

void OnMenuItemActivated (object sender, EventArgs args)
{
        string format = Catalog.GetString ("dddd, MMMM d, h:mm tt");
        string text = DateTime.Now.ToString (format);

        NoteBuffer buffer = Note.Buffer;
        Gtk.TextIter cursor = buffer.GetIterAtMark (buffer.InsertMark);
        buffer.InsertWithTagsByName (ref cursor, text, "datetime");
}

Compiling

Compiling using Mono's gmcs is as simple as (this all assume's the current directory is $HOME/.tomboy/addins):

El primer que he de fer és instal.lar el mono

$ cd ~/.tomboy/addins
$ sudo apt-get install mono-devel
$ gmcs -debug -out:InsertDateTime.dll -target:library -pkg:tomboy-addins -r:Mono.Posix InsertDateTime.cs -resource:InsertDateTime.addin.xml
$ sudo cp InsertDateTime.dll /usr/lib/tomboy/addins/InsertDateTime.dll

Funciona, i efectivament es crea dins del directori addins/ la llibreria InsertDateTime.dll. A /usr/lib/tomboy/addins/ és on estan totes les llibreries, la copio aquí.

Testing

The easiest way to test is copying the generated .dll to .tomboy/addins/ inside your home directory ($HOME). After that restart tomboy and the plugin should be automatically loaded, but not yet enabled. To enable it, go to the Addins tab in the configuration window.

It its also useful to run tomboy from command line to check its log messages.

No acaba de funcionar, pels pèls. Quan executo en la línia de comandes:

$ tomboy

veig com es carreguen totes les llibreries, i precisament quan arriba a la que he compilat peta:

WARNING: The add-in 'Tomboy.DateTimeAddin,0.1' is trying to extend '/Tomboy/NoteAddins', but there isn't any compatible add-in defining this extension point

Sincronització amb Android (per fer)


creat per Joan Quintana Compte, abril 2009

Eines de l'usuari
Espais de noms
Variants
Accions
Navegació
IES Jaume Balmes
Màquines recreatives
CNC
Informàtica musical
joanillo.org Planet
Eines