Monday, September 11, 2006

KDE::HTMLPart GUI for tagging cloud

Everyone seems to blog about Ruby. Well, now do I too. Today I wrote three Ruby scripts to that allow me to tag my PDFs, just like I do with webpages using my del.icio.us account, something I wanted to do for some time already. In KDE4 keyword support mockups I suggested to do this with xattr, so I did. This is using a ruby xattr wrapper.

thisisabout


The first script allows adding a keyword. It is a simple script and allows adding one keyword per call. Syntax: thisisabout [tag] [file]. The code:

#!/usr/bin/ruby

# Copyright (C) 2006 Egon Willighagen
# License: GPL

require 'xattr'

tag = ARGV[0]
file = ARGV[1]

if (file == nil)
p "syntax: thisisabout [TAG] [FILE]"
exit
else
description = File.get_attr(file, "keywords")
if (description != nil)
words = description.split
if (words.include?(tag))
p file + " already has the tag " + tag
else
description = description + " " + tag
end
else
description = tag
end
File.set_attr(file, "keywords", description)
end

thisisnotabout


Removing a tag is done with a similar script:

#!/usr/bin/ruby

# Copyright (C) 2006 Egon Willighagen
# License: GPL

require 'xattr'

tag = ARGV[0]
file = ARGV[1]

if (file == nil)
p "syntax: thisisnotabout [TAG] [FILE]"
exit
else
description = File.get_attr(file, "keywords")
newdescription = ""
if (description != nil)
words = description.split
words.each do |word|
if (word == tag)
# skip word
else
if (newdescription.length > 0)
newdescription = newdescription + " "
end
newdescription = newdescription + word
end
end
else
description = tag
end
File.set_attr(file, "keywords", newdescription)
end

whatsthisabout


The third script is where KDE comes in. It has a --nogui option if you don't want to GUI too show up. The code:

#!/usr/bin/ruby

# Copyright (C) 2006 Egon Willighagen
# License: GPL

require 'getoptlong'
require 'rdoc/usage'
require 'Korundum'
require '/home/egonw/bin/whatsthisabout'

class MainWindow < KDE::MainWindow

def initialize( name, counts )
super(nil, name)
setCaption("What's This About??")

vbox = Qt::VBox.new( self )
@browser = KDE::HTMLPart.new( vbox )

setCentralWidget(vbox)

@browser.begin()
counts.each {|key, value|
fontSize = (6*value)/2
@browser.write("");
@browser.write( key )
@browser.write("
");
}
@browser.end()
end

end

opts = GetoptLong.new(
[ '--nogui', '-n', GetoptLong::NO_ARGUMENT ],
[ '--help', '-h', GetoptLong::NO_ARGUMENT ]
)

startPath = nil
gui = "yes"
opts.each do |opt, arg|
case opt
when '--help'
RDoc::usage
when '--nogui'
gui = "no"
end
end

if ARGV.length == 1
startPath = ARGV[0]
elsif ARGV.length == 0
startPath = '.'
else
RDoc::usage
exit 0
end

wat = WhatsThisAbout.new
counts = wat.getKeywords(startPath)

if (gui == "yes")
about = KDE::AboutData.new("whatsthisabout", "What's This About??", "0.1")
KDE::CmdLineArgs.init(about)

a = KDE::Application.new()

window = MainWindow.new( "What's This About??", counts )
window.resize( 600, 300 )

a.mainWidget = window
window.show

a.exec
else
counts.each do |bla, bla2|
puts bla + " " + bla2.to_s
end
end

The obligatory screenshot:


In due time I will put this in KDE SVN, but I need to make a reasonable cmake script yet. Anyone who can tell me how I can have cmake check wether the required Ruby libraries are installed? There are other things to do too:

  1. make a backup/restore facility
  2. use a database instead of recursively finding tags

Because I have my PDFs in a SVN repository and share them between some work places, the first todo would mean I could share my tags too. The second would just mean a serious speedup.

4 comments:

StyXman said...

what about making it accept several tags at the same time?

thisisabout file tag1 tag2 ...

liquidat said...

Do you have any plans to import this into KDE 4 as a main system.

Or, else said: are there any (real) attempts to include tagging into KDE 4? Is there any work going on for example together with the Tenor or strigi guys?

Aaron J. Seigo said...

for a storage system, have you thought about using strigi?

(seems liquidat had a similar idea =)

Egon Willighagen said...

styxman: yes, that's one of the things I want to do.

liquidat: yes, I plan to upload these things to trunk/playground, and start better KDE integration from there. Need to make the cmake files first.

liquidat, aaron: I'm in good contact with Jos, and I have some patches in Strigi. The current situation is that Strigi will not do persistent keyword tagging, but a db backend is not unlikely.

BTW, Strigi does already index the xattr one adds, so Strigi integration is already present. When you have an inotify-enabled Strigi installed, added keywords should automatically be indexed, though I have not practically tried this.