Calibre E-book – Authoring Plugins with a Custom Context Menu

I’ve been hacking with the Calibre ebook reader for a few hours, and I’d like to share with you some notes of how to add a custom context menu to a book, so that we can open a secondary folder where the book has been exported to.

A good place to start is to download the interface_demo.zip from the Plugins documentation. Unzip the directory, and run Calibre in debug mode from that directory.

mkdir demo
cd demo
unzip interface_demo.zip

REM loads calibre plugin from the current directory
../calibre-customize.exe  -b .

REM launch calibre gui in debug mode
../calibre-debug.exe -g

Next, add the custom action to the context menu manually through the preference toolbar. I’ve taken some screenshots below:

That done, we can go in and hack on the plugin until it works.

In my particular case, I’ve got an alternate filepath stored in a custom metadata field, and I’d like to open the alternate containing folder.

#!/usr/bin/env python
# vim:fileencoding=UTF-8:ts=4:sw=4:sta:et:sts=4:ai

__license__   = 'GPL v3'
__copyright__ = '2014 Chui Tey teyc@cognoware.com'
__docformat__ = 'restructuredtext en'


from calibre.gui2.actions import InterfaceAction
import os

class InterfacePlugin(InterfaceAction):

    name = 'Open Oracle Folder'
    action_spec = ('Open Oracle folder', 'images/blank.png',
                   'Open the file in the Oracle folder', 'CTRL+O')
    dont_add_to = frozenset(['context-menu-device'])
    action_type = 'current'

    def genesis(self):
        #icon = get_icons("images/blank.png")
        #self.qaction.setIcon(icon)
        #self.qaction.setEnabled(True)
        self.qaction.triggered.connect(self.open_folder)

    def open_folder(self):
        rows = self.gui.current_view().selectionModel().selectedRows()
        if not rows or len(rows) == 0:
            return
        not_found = []
        for i, row in enumerate(rows):
            if i > 5: continue
            mi = self.gui.library_view.model().db.get_metadata(row.row())
            filepath = mi.get('#filepath', None)
            if filepath:
                import os
                folderpath = os.path.split(filepath)[0]
                if os.path.isdir(folderpath):
                    os.startfile(folderpath)
                else:
                    not_found.append(folderpath)
        if not_found:
            if len(not_found) > 1:
                raise Exception("Folders %s not there!" % ", ".join(not_found))
            else:
                raise Exception("Folder %s not there!" % ", ".join(not_found))

            # actual path
            # path = self.gui.library_view.model().db.abspath(row.row())

    def location_selected(self, loc):

        enabled = loc == 'library'
        self.qaction.setEnabled(enabled)

Learning Cobol Round #1

My mum learnt COBOL and FORTRAN when she was in university back in the 60s. Although I have used FORTRAN77 in a professional capacity, I still haven’t had the opportunity to try COBOL.

Since being retro is the harbinger of cool, I’ll spend the next few articles blogging about my experience working with COBOL.

Visual Cobol

There is a free non-commercial use license granted by Visual Cobol. This is sufficient for my learning purposes.

In terms of resources, Teach yourself COBOL is 21 days seems to be a good starting point.

The download is a little tricky. After receiving the confirmation email, I was presented with a screen but the download link wasn’t apparent. It’s tucked under the Software/Licenses tab (see red arrow). The download was 417MB, and I was a little concerned I’ll not have much space left on my SSD, which is burgeoning with several virtual machines. However, the promise of the familiarity of Visual Studio and having access to the .NET framework beckons me to try it out.

In comparison, the OPENCOBOL 1.1 source download is 1 Mb. That’s very inviting, although I don’t really like the idea of building GNU autoconf project on Windows.  There’s never a nice ending.  I found prebuilt-binaries for OPENCOBOL on Windows, and given the download size of 7.3 Mb,  I downloaded it as well.

Upon creating the first COBOL project with Visual Studio, I was prompted for a license key. It wasn’t immediately apparent, but all I had to do was to provide the email address I had used to register with Microfocus, and that activated my free copy automatically.

Running Hello World from a Visual Cobol Console project was simple enough.

       identification division.
       program-id. Program1.

       environment division.
       configuration section.

       data division.
       working-storage section.

       procedure division.

       PROGRAM-BEGIN.      
       DISPLAY "Hello World".
       PROGRAM-DONE.
       
       end program Program1.

OpenCobol

OpenCobol was straightforward. Unzip the 64bit binaries to C:\OpenCobol, and run vcvars64.bat to set up the path to Visual C (OpenCobol translates COBOL to C and then uses the platform compiler to build executables). The command below builds hello.exe

cobc -x hello.cob

Project scope

In terms of project, I’d like to try my standard project, involving authorization, data validation and persistence (both NoSQL and SQL). I’m mindful that my projects will be un-COBOL-like, but that can be refined with time.

Next article – Round #2

Powershell equivalent of xargs

I wanted to be able to build extmap.xml for all the files that were in my packages\ directory. I had a Powershell script called ToExtMap which automated most of my tasks, and a little experimentation with Powershell gave me this one liner:


PS C:\Users\Chui\Projects\SAM-GET\packages>
Get-ChildItem -Filter *.dll -Recurse -name |
%{ ToExtMap $_ | Out-File ($_ -replace "dll", "extmap.xml") -encoding ASCII}

A bit of explanation might be in order:

  1. Get-ChildItem -Recurse finds all the files I require.
  2. The %{} is the shortcut for the foreach operator.
  3. Powershell is object oriented. $_ -replace ... executes a string substitution

Over at Forbes

It started with making one presentation slide for each idea. Each slide would have a heading and several bullet points defining the scope of the feature and metrics describing what success would look like and how it would impact the business. That’s all people got to make their case. And given the speed of the whole process, there was little time for precision.

Dell Service Drop off Brisbane

The Dell service center in Brisbane has moved to 8 Castlemaine St, Cooparoo (effective 6 Jan 2014). It is in the big courier’s shed, and there’s a small narrow storefront. If you need to drop off your computer for repairs, it is no longer at Mowbray Tce.

Hopefully this post finds you before you get lost.

Piano Teacher in Toowoomba

My kids have been taking piano lessons with Marie Kilpatrick in Toowoomba and I liked her style of communicating musical ideas to the children. For example, in Mozart’s Andante, she likened the left hand to rustling wind, while a diva sings on the right hand, and then she works through with the children the kind of touch necessary. Another way she communicated this is by explaining that the triplets on the left hand outnumber the melody on the right hand, and therefore has to be much much softer.