<chapter id="hacking">
<title>Hacking &appname;</title>

<para>
In the spirit of Free Software, you are welcome to hack on &appname; as much as you like. You should be able to write scripts to import, export, or modify data very easily. This chapter gives you more information about doing that.
</para>

<sect1 id="file-format">
<title>File Format</title>

<para>
The default &appname; data file is a zip archive, normally with a <literal role="extension">.tc</literal> file extension. Inside the archive is a top-level <filename>tellico.xml</filename> file. Images may be included inside the <filename>images/</filename> folder in the archive, or they may be included in the &XML; data directly in a base64 encoding. Images may also be saved inside the application data folder, in which case, they are not in the data file at all. &appname; can also load the &XML; file, by itself, uncompressed.
</para>

<sect2 id="xml-format">
<title>&XML; Data</title>

<para>
</para>

<sect3 id="coll-xml-data">
<title>Collection</title>
<programlisting>
<![CDATA[
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE tellico PUBLIC "-//Robby Stephenson/DTD Tellico V11.0//EN" "http://periapsis.org/tellico/dtd/v11/tellico.dtd">
<tellico xmlns="http://periapsis.org/tellico/" syntaxVersion="11">
 <collection title="My Books" type="2">
 </collection>
</tellico>
]]>
</programlisting>

<para>
The file begins with the required &XML; declaration and encoding, followed by the doctype. When a new field type is added or additional properties are set on the default fields, the doctype DTD version is incremented. &appname; is always able to open and read any previous DTD version, but will save files in the current version. The DTD location does point to an actual DTD file.
</para>

<para>
The top-level element is a <markup>&lt;tellico&gt;</markup> element, containing the default namespace declaration and the syntax version of the file, which should always match the DTD.
</para>

<para>
The <markup>&lt;tellico&gt;</markup> element contains one <markup>&lt;collection&gt;</markup> element. Multiple collections are ignored, for now. The <markup>title</markup> attribute contains the collection title, while the <markup>type</markup> specifies what kinds of entries are contained in the collection. The allowable types are in <link linkend="collection-type-values">listed in a later section</link>. An optional <markup>entryTitle</markup> attribute may be used to specify the title of the entries for a custom collection, and should be plural.
</para>
</sect3>

<sect3 id="fields-xml-data">
<title>Fields</title>

<programlisting>
<![CDATA[
  <fields>
   <field flags="8" title="Title" category="General" format="1" type="1" name="title" />
   <field flags="7" title="Author" category="General" format="2" type="1" name="author" />
   <field flags="2" title="Binding" category="General" allowed="Hardback;Paperback;Trade Paperback;E-Book;Magazine;Journal" format="4" type="3" name="binding" >
    <prop name="default">Paperback</prop>
   </field>
   <field flags="6" title="Publisher" category="Publishing" format="0" type="1" name="publisher" />
   <field flags="4" title="Edition" category="Publishing" format="0" type="1" name="edition" />
   <field flags="3" title="Copyright Year" category="Publishing" format="4" type="6" name="cr_year" />
   <field flags="2" title="Publication Year" category="Publishing" format="4" type="6" name="pub_year" />
   <field flags="0" title="ISBN#" category="Publishing" format="4" type="1" name="isbn" description="International Standard Book Number" />
   <field flags="7" title="Genre" category="Classification" format="0" type="1" name="genre" />
   <field flags="7" title="Keywords" category="Classification" format="0" type="1" name="keyword" />
   <field flags="0" title="Front Cover" category="Front Cover" format="4" type="10" name="cover" />
   <field flags="0" title="Comments" category="Personal" format="4" type="1" name="comments" />
   <field title="Rating" flags="2" category="Personal" format="4" type="14" name="rating">
    <prop name="maximum">5</prop>
    <prop name="minimum">1</prop>
   </field>
   <field title="ID" flags="32" category="Personal" format="4" type="6" name="id">
    <prop name="template">%{@id}</prop>
   </field>

  </fields>
]]>
</programlisting>

<para>
All of the fields are defined inside a <markup>&lt;fields&gt;</markup> element, of which there can be only one. All of the information for a field, except for any properties, are included as attributes of the <markup>&lt;field&gt;</markup> element. The allowable values for the <markup>flags</markup>, <markup>format</markup>, and <markup>type</markup> attributes are given in a <link linkend="field-type-values">following section</link>.
</para>

<para>
Field properties are used for setting default field values, rating value ranges, derived value templates, etc. The examples above include a default value, a maximum rating value, and a template for a derived ID field.
</para>

<para>

</para>

</sect3>

<sect3 id="entries-xml-data">
<title>Entries</title>

<programlisting>
<![CDATA[
  <entry>
   <title>C++ Programming Language, The</title>
   <authors>
    <author>Stroustrup, Bjarne</author>
   </authors>
   <publisher>Addison-Wesley Pub Co</publisher>
   <edition>3rd</edition>
   <pub_year>1997</pub_year>
   <isbn>0-201-88954-4</isbn>
   <genres>
    <genre>Non-Fiction</genre>
   </genres>
   <keywords>
    <keyword>Programming</keyword>
    <keyword>Computers</keyword>
   </keywords>
   <cover>cf65a2f023b6cb9e9233323dca10ac7c.jpeg</cover>
  </entry>
]]>
</programlisting>

<para>
For every field in the collection, an <markup>&lt;entry&gt;</markup> may contain an element whose name is identical to the field name. If multiple values are allowed for the field, then the letter <emphasis>s</emphasis> is added to the field name to create an element, and each value is added as a child of the element, as in the case of the author, genre, and keyword fields above.
</para>

<para>
As a result, if additional fields are added to the collection, the data file will no longer conform to the DTD. However, &appname; uses a non-validating &XML; parser, so additional fields do not cause problems.
</para>
</sect3>

<sect3 id="images-xml-data">
<title>Images</title>
<programlisting>
<![CDATA[
  <images>
   <image width="111" format="JPEG" height="140" id="cf65a2f023b6cb9e9233323dca10ac7c.jpeg" />
  </images>
]]>
</programlisting>

<para>
Inside the <markup>&lt;images&gt;</markup> element, each image referenced by an entry is listed, along with attributes describing the image's size, format, and id. If the image is contained inside the Zip file, the element is empty. Otherwise, the image data may be contained in the &XML; stream as base64-encoded text.
</para>
</sect3>

</sect2>

</sect1>

<sect1 id="collection-type-values">
<title>Collection Type Values</title>

<para>
The type of collection is given in the type attribute of the collection element. The value is equal to the <type>Type</type> enum value in <filename>src/collection.h</filename>.
</para>

<table>
<title>Collection Type Values</title>
<tgroup cols="2">
<thead>
<row>
<entry>Collection Type</entry>
<entry>Value</entry>
</row>
</thead>
<tbody>
<row><entry>Custom Collection</entry><entry>1</entry></row>
<row><entry>Book Collection</entry><entry>2</entry></row>
<row><entry>Video Collection</entry><entry>3</entry></row>
<row><entry>Music Collection</entry><entry>4</entry></row>
<row><entry>Bibliography</entry><entry>5</entry></row>
<row><entry>Comic Book Collection</entry><entry>6</entry></row>
<row><entry>Wine Collection</entry><entry>7</entry></row>
<row><entry>Coin Collection</entry><entry>8</entry></row>
<row><entry>Stamp Collection</entry><entry>9</entry></row>
<row><entry>Trading Card Collection</entry><entry>10</entry></row>
<row><entry>Video Game Collection</entry><entry>11</entry></row>
<row><entry>File Catalog</entry><entry>12</entry></row>
<row><entry>Board Game Collection</entry><entry>13</entry></row>
</tbody>
</tgroup>
</table>

</sect1>

<sect1 id="field-type-values">
<title>Field Type Values</title>

<para>
&appname; will include all the default fields for a collection if the first field element has the name <emphasis>_default</emphasis>. For <emphasis>Paragraph</emphasis>, <emphasis>Table</emphasis>, or <emphasis>Image</emphasis> fields, the field category should be identical to the field title.
</para>

<para>
The type of field is given in the type attribute of the field element. The value is equal to the <type>FieldType</type> enum value in <filename>src/field.h</filename>.
</para>

<table>
<title>Field Type Values</title>
<tgroup cols="2">
<thead>
<row>
<entry>Field Type</entry>
<entry>Value</entry>
</row>
</thead>
<tbody>
<row><entry>Simple Text</entry><entry>1</entry></row>
<row><entry>Paragraph</entry><entry>2</entry></row>
<row><entry>Choice</entry><entry>3</entry></row>
<row><entry>Checkbox</entry><entry>4</entry></row>
<row><entry>Number</entry><entry>6</entry></row>
<row><entry>&URL;</entry><entry>7</entry></row>
<row><entry>Table</entry><entry>8</entry></row>
<row><entry>Image</entry><entry>10</entry></row>
<row><entry>Date</entry><entry>12</entry></row>
<row><entry>Rating</entry><entry>14</entry></row>
</tbody>
</tgroup>
</table>

<para>
The field may have different flags set, given as a bitwise OR'd value in the flags attribute on the field element. The flag for preventing the user from deleting a field is intended for such things as the citation key for bibliographic entries.
</para>

<table>
<title>Field Flag Values</title>
<tgroup cols="2">
<thead>
<row>
<entry>Field Flags</entry>
<entry>Value</entry>
</row>
</thead>
<tbody>
<row><entry>Allow Multiple Values</entry><entry><constant>0x01</constant></entry></row>
<row><entry>Allow Grouping</entry><entry><constant>0x02</constant></entry></row>
<row><entry>Allow Completion</entry><entry><constant>0x04</constant></entry></row>
<row><entry><emphasis>Disallow Deleting</emphasis></entry><entry><constant>0x08</constant></entry></row>
<row><entry><emphasis>Disallow Editing</emphasis></entry><entry><constant>0x10</constant></entry></row>
<row><entry>Derived Value</entry><entry><constant>0x20</constant></entry></row>
</tbody>
</tgroup>
</table>

<para>
The format of the field is given in the format attribute on the field element. <emphasis>Date Formatting</emphasis> is not currently used. Grouping by <emphasis>People</emphasis> uses all the fields which use <emphasis>Name Formatting</emphasis>. Setting the <emphasis>Derived Value</emphasis> flag implies the value for the field is generated from the template property from other field values.
</para>

<table>
<title>Field Format Values</title>
<tgroup cols="2">
<thead>
<row>
<entry>Field Format</entry>
<entry>Value</entry>
</row>
</thead>
<tbody>
<row><entry>Capitalization Only</entry><entry>0</entry></row>
<row><entry>Title Formatting</entry><entry>1</entry></row>
<row><entry>Name Formatting</entry><entry>2</entry></row>
<row><entry><emphasis>Date Formatting</emphasis></entry><entry>3</entry></row>
<row><entry>No Formatting</entry><entry>4</entry></row>
</tbody>
</tgroup>
</table>

</sect1>

<sect1 id="hidden-options">
<title>Hidden Configuration Options</title>

<para>
&appname; has some additional configuration options that are not visible in the <interface>Configuration Dialog</interface>. They are not important enough to warrant cluttering the dialog with more settings, but as they represent options which may appeal to various users, the application does read them from the configuration file.
</para>

<para>
The settings for &appname; are saved in a file in the user's home folder, typically <filename>~/.config/tellicorc</filename>. Within that file, settings are put in groups, which appear with bracketed names, such as [General Options]. To add a setting to the <emphasis>General Options</emphasis> group, find the line in the settings file with that group name. If it does not appear, then create it yourself by adding a line saying [General Options]. Then, the setting can be added underneath the group name.
</para>

<sect2 id="hidden-general-options">
<title>[General Options]</title>

<para>
These settings should be placed in the <emphasis>General Options</emphasis> group.
</para>

<sect3>
<title>Max Icon Size</title>

<para>
The maximum size of the icons in the <interface>Icon View</interface> may be changed with this setting. The default value is 96. The size must be in the range of 32 to 512.
</para>
</sect3>

<sect3>
<title>Example</title>
<informalexample>
<para><userinput>Max Icon Size=128</userinput></para>
</informalexample>
</sect3>

<sect3>
<title>Icon Cache Size</title>

<para>
The maximum numbers of icons cached in memory may be changed with this setting. The default value is 1000.
</para>
</sect3>

<sect3>
<title>Example</title>
<informalexample>
<para><userinput>Icon Cache Size=100</userinput></para>
</informalexample>
</sect3>

<sect3>
<title>Image Cache Size</title>

<para>
The maximum amount of memory in bytes used for caching all the images may be changed with this setting. The default value is 512 MiB.
</para>
</sect3>

<sect3>
<title>Example</title>
<informalexample>
<para><userinput>Image Cache Size=256000000</userinput></para>
</informalexample>
</sect3>

</sect2>

<sect2 id="hidden-bibtex-options">
<title>[Options - bibtex]</title>

<para>
These settings should be placed in the <emphasis>Options - bibtex</emphasis> group.
</para>

<sect3>
<title>lyxpipe</title>

<para>
This setting is for the location of the lyxpipe for sending bibliographic citations. It should not include the <literal role="extension">.in</literal> suffix.
</para>
</sect3>

<sect3>
<title>Example</title>
<informalexample>
<para><userinput>lyxpipe=$HOME/.lyx/lyxpipe</userinput></para>
</informalexample>
</sect3>
</sect2>

<sect2 id="file-reader-options">
<title>[File Reader Options]</title>

<para>These settings should be placed in the <emphasis>File Reader Options</emphasis> group.</para>

<sect3>
<title>Preview Size</title>

<para>This setting is for the maximum size of the generated file preview.</para>
</sect3>

<sect3>
<title>Example</title>
<informalexample>
<para><userinput>Preview Size=523</userinput></para>
</informalexample>
</sect3>
</sect2>

<sect2 id="multi-disc-options">
  <title>Multi-Disc Options</title>

  <para>The <link linkend="musicbrainz">MusicBrainz</link>, <link linkend="discogs">Discogs</link>, and
    <link linkend="itunes">iTunes</link> data sources can fetch multi-disc album information, specifying
    which tracks each disc contains. By default, the track lists are inserted into separate field, one
    for each disc. Prior to version 4.0, the track lists were combined. This option provides the pre-4.0
    behavior on an individual data source level. Add it to each <emphasis>Data Source</emphasis> section
    where it is desired.</para>

  <sect3>
    <title>Split Tracks By Disc</title>

    <para>Setting this to <emphasis>false</emphasis> reverts to pre-4.0 behavior where the tracks are collapsed into a single track list.</para>
  </sect3>

  <sect3>
    <title>Example</title>
    <informalexample>
      <para><userinput>Split Tracks By Disc=false</userinput></para>
    </informalexample>
  </sect3>
</sect2>

</sect1>

<sect1 id="bibtex-translation">
<title>Bibtex Character Translation</title>

<para>
When bibtex files are imported or exported, certain characters are translated between their TeX equivalents and the Unicode characters. Those character mappings are contained in the <filename>bibtex-translation.xml</filename> file, located in the installation data directory. The mappings can be modified, as desired. The key element contains the Unicode character, and the string elements within contain the TeX equivalents, which may be a one-to-many mapping. The first one is the one used when exporting to bibtex.
</para>

<programlisting>
<![CDATA[
  <key char="À">
    <string>{\`A}</string>
    <string>\`{A}</string>
  </key>
]]>
</programlisting>

</sect1>

</chapter>
