|07-20-2012, 11:03 PM||#1|
Join Date: Dec 2011
Mobi Metadata Reader .Net (C# / .Net 4.0)
As promised (erm, several months ago, sorry for the delay!) I have knocked up a Mobi metadata reader in C#. Since several people expressed an interest in seeing the code for this if I got it up and running, I thought I'd post it here.
I had actually written this as part of a larger ebook management system, quite specific to my Dad's needs to help rename and tidy his eBook files, so I have extracted my code from this and made this standalone Windows application.
This has been written entirely in C#, including all the parsing of the Mobi metadata file, so it is not reliant on Mobiperl or any other external modules or libraries. All the mobi metadata stuff is in a separate folder so that one can easily copy this folder into any other .Net application to use all of its functionality.
I only needed read access and also didn't want to mess with writing to Mobi metadata at this stage (I understand you can seriously screw up the Mobi file if you get it wrong!) so this is just read only. However, I don't imagine there would be too much work involved to extend this program to write metadata back to the mobi file.
The zip file contains the full source and I have built debug and release versions of the .exe, so if you don't have Visual Studio simply go into the debug or release folders to run the .exe. You will, of course, need the .Net 4.0 Framework installed.
I consider this to be open source, so please feel free to use or reuse all or part of this in your own applications, as long as it's not for commercial gain. Just please throw me a credit if you use it and post it anywhere!
Additionally I'd like to credit the following sources:
Java Mobi Metadata Editor by Gluggy
The "guts" of the Mobi file parsing has been heavily modelled on Gluggy's Java program, so full credit to him for that!
Windows Explorer User Control by Raj Lal
I used (and tweaked) this Windows Explorer user control for the user interface
PLATFORMS AND REQUIREMENTS
- Windows 7, Window Vista or Windows XP.
NOTE: You might possibly get this working on earlier operating systems but I'm not prepared to support anything older than XP! ;p (so Windows 95/98/NT4/2000 users, if there are any out there, will have to figure any problems out for yourselves!) ;p
- .Net 4.0 Framework to run the executables.
- Microsoft Visual Studio 2010 required to compile the source.
NOTE: With some minor tweaking to the using statements in the source one could probably get this running under at least .Net 3.5, and possibly .Net 2.0 and build in Visual Studio 2008/2005. Earlier than 2.0/2005 would involve extensive rewriting due to the use of typed lists throughout the application.
Building and testing platform:
- Written with Visual Studio 2010 and built/tested on Windows 7 Ultimate, 64-bit OS.
I have tested this on a few dozen of my own ebook folders and all work fine, but obviously a wider test may prove a few hitches with a few books.
I've tried to adopt a fairly simple Windows Explorer style interface. The home folder by default is C:\ but one can easily set this to be the root of the ebook collection just by clicking the house button at the top of the form.
Then at any time you can go back to the home folder by clicking the house button on the explorer bar.
The left hand pane has a Windows Explorer treeview for folder selection.
The right hand pane shows the mobi metadata for every supported file in the folder.
Currently only .mobi and .prc files are supported.
The controls should all be anchored to the edges so you can resize the main form as you wish to fit your own screen resolution. This is also a splitter control between the left hand explorer and right-hand grid, so you can adjust this for best viewing depending on your file structure and the number of columns in the grid that you want to view.
The columns be can resized and the data can be ordered by clicking the desired column.
There are two options for this grid available by right-clicking any book listed, and also by clicking either button on the explorer bar.
View full metadata (puzzle piece icon)
This will display a dialog box with all the metadata for this ebook. It splits the headers into tabs and shows a grid on each tab with the data. On the final tab there is a plain text rendition of the metadata
Configure columns and settings (cogwheel icon)
This displays a dialog box which lists all available unchosen supported columns for the metagrid, as well as listing which columns have been chosen. Simply double click any of the columns to chose it for the grid, and double click any chosen column on the right hand list to unchose it. As well as all the supported Mobi metadata fields, I have added three system columns (filename, size and date created). It would be a small matter to add additional system fields should they be needed.
There is also a checkbox option to autosize the columns. This will set each column to either width of its longest piece of data, or its column name, whichever is longer. But also there is a max width in pixels which can be set. This is handy for long description/review type data where you don't want the column width to autosize to several thousand pixels width, but you want the rest of the data to be sized properly. I find around 350 pixels to be good max size but you can play around with this setting.
There is also an About (info icon) button on the top bar which displays the about box, including the full application history. This information is pulled from the assembly manifest and the History/History.txt file in the project.
All settings such as current folder, home folder, all the columns selected and their widths, and the sort column name and order, are serialized to XML and stored in isolated storage to persist these when you close the application, and load when you open the application. At any time you can reset to default (C:\ home folder and default columns etc) by clicking the restore button at the top.
All the classes for parsing the Mobi metadata are in the Metadata subfolder in the project. One should easily be able to take this folder of classes and insert it into another project/application and reuse these classes with the minimum of effort. Just a question of renaming the namespaces for each class to your own.
MobiMetadata - this is the main object class, and has the main header objects as properties.
To instantiate the MobiMetadata class one simply uses the overloaded constructor, either by passing the file path as a string, which will internally create a FileStream object with which to instantiate the subclasses, or by passing in a FileStream object directly. From this main object one can that access the header object properties, and strongly typed field properties from those.
MobiMetadata mobi = new MobiMetadata(@"C:\MyEBooks\The Time Machine.mobi");
string fileName = mobi.PDBHeader.Name;
uint fileVersion = mobi.MobiHeader.FileVersion;
string authorName = mobi.MobiHeader.EXTHHeader.Author;
string updatedTitle = mobi.MobiHeader.EXTHHeader.UpdatedTitle;
myDataGrid.DataSource = mobi.MobiHeader.EXTHHeader.FieldListNoBlanks;
myMultilineTextBox.Text = mobi.PalmDOCHeader.ToString();
All header objects inherit from a base header, which has several SortedDictionary properties. These can be used to automatically set, for example, the DataSource of a grid to the data within one of these headers. The Windows interface provided uses this to populate the grids when viewing the full metadata.
There are 3 of these SortedDictionary properties
FieldList - full, populate field list of the header
FieldListNoBlanks - will populate only with non-blank fields (handy for getting only EXTH Header records that have been populated)
EmptyFieldList - handy for getting a list of supported fields without actually having to populate this from a .mobi file. This was used to get selectable field list for the grid.
Also, all the headers have a ToString() override to serve as a quick and easy way to get the list of properties and values in a plain text format.
Anyway, that's it for now! I've set this as a beta version 0.1.0.0 since I suspect there'll be a few glitches which will need ironing out, as I've only had chance to do minimal testing.
Last edited by Limey; 07-23-2012 at 12:12 PM.
|Thread Tools||Search this Thread|
|Thread||Thread Starter||Forum||Replies||Last Post|
|E-Reader Testvergleich auf lesen.net||Ulli||Deutsches Forum||0||04-28-2010 04:48 PM|
|Is It Possible To Read Books From Net Libraries on the Sony Reader?||Vienna01||Sony Reader||3||03-02-2010 09:22 AM|
|Metadata problems from manybooks.net||morpheus||Sony Reader||5||03-09-2008 12:25 AM|
|Need E Reader that Surfs the Net||MickeyC||Which one should I buy?||3||01-21-2008 06:15 PM|