The collections are defined by an external file which looks something like this:
Code:
MemoryType=int
Recursive=1
FileNameRegExp=\.(lrf|pdf|txt|rtf|epub)$
Cache=database\cache\media.xml
BooksPath=database\media\books\
BooksBasePath=database/media/books/
[Collection of Something]
something\book1.pdf
something\book2.lrf
something\book3.lrf
[My Favorite Books]
something\book1.pdf
foo\bar.txt
cool.rtf
It could be modified to work directly with directory structure via regexps, probably easily enough. At the moment it seemed more useful to go for a list-based approach, as it allows placing a book into several collections and ordering the collection correctly.
Windows-specific stuff - there is a CreateProcess call to execute Calibre's whatever-META.EXE and capture its output, but I am sure that could be easily rewritten for Linux. The biggest obstacle will be the XML library I used - it seemed like a lot of work just to make it compile under FreePascal.