Source of MobiEdit

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
//
using MobiLibrary;
using System.IO;

namespace MobiEdit
{
    public partial class Form1 : Form
    {
        MobiLib ml = new MobiLib();
        int[] superfluous = { 1, 2, 3, 103, 104, 105, 107, 108, 113, 208, 300, 403, 503 };
        CCtrlZ z;
        //
        public Form1()
        {
            InitializeComponent();
            //
            z = new CCtrlZ(this);
            //
            this.Text = "";
            setLocale();
            //
            listBox_LC.DisplayMember = "Name";
            listBox_LC.ValueMember = "Lcid";
            listBox_LC.DataSource = MobiLibrary.LanguageCodes.GetLangs();
            //
            listBox1.DisplayMember = "OneLine";
            string file;
            if ("" != (file=Environment.GetEnvironmentVariable("___MOBIPATH")))
            {
                loadFile(file);
            }
        }

        private void button_Save_Click(object sender, EventArgs e)
        {
            button_Save.Enabled = false;
            //
            if (!ml.IsDRM)
            {
                ml.ConvertToStandardIfNeeded = checkBox_ConvertIfNeeded.Checked;
                //
                ml.Name = textBox_Name.Text;
                ml.FullName = textBox_FullName.Text;
                if (ml.sLocale != textBox_Locale.Text) ml.Locale = LanguageCodes.Name2LCID(textBox_Locale.Text);
                if (ml.IsEXTH) ml.exthAuthor = textBox_Author.Text;
                ml.WriteFile();
            }
            //
            button_Save.Enabled = true;
        }

        private void Form1_DragEnter(object sender, DragEventArgs e)
        {
            e.Effect = DragDropEffects.Copy;
        }

        private void Form1_DragOver(object sender, DragEventArgs e)
        {
            e.Effect = DragDropEffects.Copy;
        }

        private void Form1_DragDrop(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.FileDrop))
            {
                //
                e.Effect = DragDropEffects.Copy;
                //
                string file = ((string[])e.Data.GetData(DataFormats.FileDrop))[0];
                //
                if (!Directory.Exists(file))//zapewne to plik
                {
                    if (File.Exists(file))
                    {
                        loadFile(file);
                    }
                    else
                    {
                        e.Effect = DragDropEffects.None;
                    }
                    return;
                }
                if (Directory.Exists(file))
                {
                    e.Effect = DragDropEffects.None;
                    return;
                }
                else
                {
                    e.Effect = DragDropEffects.None;
                }
                //
            }
        }

        private void button_LC_Click(object sender, EventArgs e)
        {
            textBox_FullName.Text = Utils.ToSentence(textBox_FullName.Text);
        }

        private void button_FLNAME_Click(object sender, EventArgs e)
        {
            if (textBox_Author.Enabled)
            {
                textBox_Author.Text = Utils.ToFullName(textBox_Author.Text);
            }
        }

        private void button_SwapFL_Click(object sender, EventArgs e)
        {
            if (textBox_Author.Enabled)
            {
                textBox_Author.Text = Utils.swapLastFirstName(textBox_Author.Text);
            }
        }

        private void button_AddComma_Click(object sender, EventArgs e)
        {
            textBox_Author.Text = Utils.addCommaAfterLastName(textBox_Author.Text);
        }

        private void checkBox_ConvertIfNeeded_CheckedChanged(object sender, EventArgs e)
        {
            ml.ConvertToStandardIfNeeded = checkBox_ConvertIfNeeded.Checked;
        }

        private void button_EXTH_Clean_Click(object sender, EventArgs e)
        {
            if (ml.IsEXTH)
            {
                //Contributor, Subject, asin, isbn
                foreach (int i in superfluous) ml.exthDeleteType(i);
                //
                ml.exthDeleteDoubles();
                listBox1.DataSource = ml.GetBindingList();
            }
        }

        private void loadFile(string file)
        {
            if (File.Exists(file))
            {
                z.Clear();
                //
                listBox1.DataSource = null;
                //
                this.Text = "";
                //
                checkBox_DRM.Checked = false;
                checkBox_ConvertIfNeeded.Checked = true;
                //
                foreach (Control c in this.Controls)
                {
                    TextBox tb;
                    if(null != (tb = c as TextBox))
                    {
                        tb.Text = "";
                    }
                }
                //
                if (!ml.ReadFile(file))
                {
                    MessageBox.Show("ERROR!", "["+Path.GetFileName(file)+"]", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
                //
                checkBox_DRM.Checked = ml.IsDRM;
                if (ml.IsDRM) checkBox_ConvertIfNeeded.Checked = false;
                //
                textBox_Author.Text = ""; textBox_Author.Enabled = false;
                listBox1.Enabled = false;
                //
                if (ml.IsEXTH)
                {
                    listBox1.Enabled = true;
                    textBox_Author.Enabled = true;
                    listBox1.DataSource = ml.GetBindingList();
                    listBox1.DisplayMember = "OneLine";
                    //
                    if (null == ml.exthAuthor) { textBox_Author.Text = ""; }
                    else { textBox_Author.Text = ml.exthAuthor; }
                }
                //
                this.Text = Path.GetFileName(file);
                //
                textBox_Locale.Text = ml.sLocaleShort;
                textBox_Name.Text = ml.Name;
                textBox_FullName.Text = ml.FullName;
                //
                z.SaveTextBoxes();
                //
            }
        }
        private void setLocale()
        {
            switch (LanguageCodes.CurrentLCID())
            {
                case 21:
                case 1045:
                    toolTip1.SetToolTip(this, "Przeciągnuj tutaj książkę...");
                    //
                    toolTip1.SetToolTip(button_AddComma, "Kowalski Jan => Kowalski, Jan");
                    toolTip1.SetToolTip(button_EXTH_Clean, "Usunięcie duplikatów i zbędnych wpisów w EXTH");
                    toolTip1.SetToolTip(button_FLNAME, "kowalski jan => Kowalski Jan");
                    toolTip1.SetToolTip(button_LC, "tytuł książki => Tytuł książki");
                    toolTip1.SetToolTip(button_Save, "Zapisz");
                    toolTip1.SetToolTip(button_SwapFL, "Jan Kowalski => Kowalski, Jan");
                    //
                    toolTip1.SetToolTip(checkBox_ConvertIfNeeded, "Czy dodać do nagłówka książki brakujące elementy");
                    toolTip1.SetToolTip(checkBox_DRM, "Czy ta książka ma DRM");
                    //
                    toolTip1.SetToolTip(textBox_Locale, "Język książki");
                    toolTip1.SetToolTip(textBox_Name, "Przestarzałe: nazwa książki");
                    toolTip1.SetToolTip(textBox_FullName, "Tytyuł książki");
                    toolTip1.SetToolTip(textBox_Author, "Autor książki");
                    //
                    toolTip1.SetToolTip(listBox1, "Zawartść EXTH");
                    toolTip1.SetToolTip(listBox_LC, "Kody języków");
                    break;
            }
            //
            toolTip1.SetToolTip(button_EXTH_Clean, toolTip1.GetToolTip(button_EXTH_Clean) + "\n" + string.Join(",", superfluous));
            //
        }

        private void listBox_LC_Click(object sender, EventArgs e)
        {
            if (listBox_LC.SelectedIndex >= 0)
            {
                textBox_Locale.Text = ((MobiLibrary.LanguageCodes.Lang)listBox_LC.SelectedValue).Lcid.ToString();
            }
        }

    }
}

Last edited Jan 4, 2012 at 9:09 AM by Miroslaw_Maczka, version 2

Comments

No comments yet.