#!/usr/bin/python
# -*- coding: utf-8 -*-
"""A simple blosxom client."""
import pygtk
pygtk.require("2.0")
import gtk
import ftplib
import time
import os
import re

settings = {}

def on_button_clicked(button):
  buffer = body.get_buffer()
  start_iter = buffer.get_start_iter()
  end_iter = buffer.get_end_iter()
  txt = buffer.get_text(start_iter, end_iter, 0)
  filename = "%i.%s" % (int(time.time()), "txt")
  message = "connecting..."
  try:
    ftp = ftplib.FTP(settings["server"])
    message = "login..."
    ftp.login(settings["userid"], settings["password"])
    message = "change directory to publish_root..."
    ftp.cwd(settings["publish_root"])
    try:
      ftp.cwd(category.get_text())
    except:
      message = "creating new directory in publish_root..."
      ftp.mkd(category.get_text())
    message = "creating temporary file in local..."
    file = open(filename, "w")
    file.write(title.get_text())
    file.write("\n")
    file.write(txt)
    file.close()
    message = "reading local temporary..."
    file = open(filename, "rb")
    message = "uploading an entry file..."
    ftp.storbinary("STOR %s" % filename, file)
    ftp.quit()
    file.close()
    message = "removing local temporary"
    os.remove(filename)
  except Exception, e:
    try:
      ftp.close()
    except:
      pass
    try:
      file.close()
    except:
      pass
    try:
      os.remove(filename)
    except:
      pass
    dialog = gtk.MessageDialog(window,
      gtk.DIALOG_MODAL | gtk.DIALOG_DESTROY_WITH_PARENT, gtk.MESSAGE_ERROR,
      gtk.BUTTONS_CLOSE,
	  message + "\n" + str(e))
    dialog.run()
    dialog.destroy()

fname = os.path.expanduser("~/.blosxom-client")
try:
  file = open(fname, "r")
  reg = re.compile("^([^=]+)=(\S*)")
  while 1:
    line = file.readline()
    if not line:
      break
    m = reg.search(line)
    settings[m.group(1)] = m.group(2)
  file.close
except:
  try:
    file.close()
  except:
    pass

window = gtk.Window()
window.set_title("Blosxom Client")
window.connect("delete-event", gtk.main_quit)
window.set_border_width(10)

vbox = gtk.VBox()
window.add(vbox)

table = gtk.Table(2, 2, False)
table.show()
vbox.add(table)

label = gtk.Label("タイトル:")
table.attach(label,
  0, 1,                   0, 1,
  gtk.FILL,  gtk.EXPAND | gtk.FILL,
  0,                      0)
title = gtk.Entry()
table.attach(title,
  1, 2,                   0, 1,
  gtk.EXPAND | gtk.FILL,  gtk.EXPAND | gtk.FILL,
  0,                      0)

label = gtk.Label("カテゴリ:")
table.attach(label,
  0, 1,                   1, 2,
  gtk.FILL,  gtk.EXPAND | gtk.FILL,
  0,                      0)
category = gtk.Entry()
table.attach(category,
  1, 2,                   1, 2,
  gtk.EXPAND | gtk.FILL,  gtk.EXPAND | gtk.FILL,
  0,                      0)

sw = gtk.ScrolledWindow()
sw.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
vbox.add(sw)

body = gtk.TextView()
body.set_size_request(500, 200)
body.queue_resize()
sw.add(body)

button = gtk.Button("公開")
button.connect("clicked", on_button_clicked)
vbox.add(button)

window.show_all()
gtk.main()

