# progress-rpminstall.py
#
# Copyright (C) 2011 - Yasumichi Akahoshi
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

from gi.repository import Gtk, GdkPixbuf, Gdk
import os, sys, time, rpm

UI_FILE = "ui/progress.ui"

class ProgressDialog:
	def __init__(self, parent, ts):
		# Build user interface
		self.builder = Gtk.Builder()
		self.builder.set_translation_domain('rpminstall')
		self.builder.add_from_file(os.path.join (os.path.dirname(__file__), UI_FILE))
		self.builder.connect_signals(self)

		self.dialog = self.builder.get_object('progress_dialog')
		self.ts = ts
		self.dialog.show()

	def run(self):
		self.ts.run (self.runCallback, None)
		return	Gtk.ResponseType.OK

	def runCallback(self, reason, amount, total, key, client_data):
		TextBuffer = self.builder.get_object('textbuffer1')
		if reason == rpm.RPMCALLBACK_TRANS_START:
			self.total = total
			self.step = 0
			label2 = self.builder.get_object('label2')
			label2.set_text( str(self.step) + '/' + str(self.total) )
			TextBuffer.insert_at_cursor("Transaction start "
				+ str(amount) + " " + str(total) + " " + str(key) + "\n")
			Gtk.main_iteration()

		elif reason == rpm.RPMCALLBACK_TRANS_PROGRESS:
			TextBuffer.insert_at_cursor("Transaction progress "
				+ str(amount) + " " + str(total) + " " + str(key) + "\n")
			Gtk.main_iteration()

		elif reason == rpm.RPMCALLBACK_TRANS_STOP:
			TextBuffer.insert_at_cursor("Transaction stop "
				+ str(amount) + " " + str(total) + " " + str(key) + "\n")
			Gtk.main_iteration()

		elif reason == rpm.RPMCALLBACK_INST_OPEN_FILE:
			TextBuffer.insert_at_cursor( "Opening file. " + str(reason) + " " + str(amount)
				+ " " + str(total) + " " + str(key) + " " + str(client_data) + "\n")
			self.rpmtsCallback_fd = os.open(str(key), os.O_RDONLY)
			label1 = self.builder.get_object('label1')
			label1.set_text(_("Install ") + key)
			Gtk.main_iteration()
			return self.rpmtsCallback_fd

		elif reason == rpm.RPMCALLBACK_INST_START:
			TextBuffer.insert_at_cursor("Install Start" + "\n")
			Gtk.main_iteration()

		elif reason == rpm.RPMCALLBACK_INST_CLOSE_FILE:
			TextBuffer.insert_at_cursor("Closing file. " + str(reason) + " " + str(amount)
				+ " " + str(total) + " " + str(key) + " " + str(client_data) + "\n")
			os.close(self.rpmtsCallback_fd)
			Gtk.main_iteration()
			self.step += 1
			progressbar2 = self.builder.get_object('progressbar2')
			progressbar2.set_fraction(float(self.step) / self.total)
			Gtk.main_iteration()
			label2 = self.builder.get_object('label2')
			label2.set_text( str(self.step) + '/' + str(self.total) )
			Gtk.main_iteration()
			if self.step == self.total:
				time.sleep(5)
				self.dialog.destroy()

		elif reason == rpm.rpm.RPMCALLBACK_INST_PROGRESS:
			progressbar1 = self.builder.get_object('progressbar1')
			progressbar1.set_fraction(float(amount) / total)
			Gtk.main_iteration()

		elif reason == rpm.RPMCALLBACK_TRANS_STOP:
			TextBuffer.insert_at_cursor("stop" + "\n")
			Gtk.main_iteration()

		elif reason == rpm.RPMCALLBACK_UNKNOWN:
			TextBuffer.insert_at_cursor("problem" + "\n")
			Gtk.main_iteration()

		elif reason == rpm.RPMCALLBACK_UNINST_PROGRESS:
			TextBuffer.insert_at_cursor("RPMCALLBACK_UNINST_PROGRESS" + "\n")
			Gtk.main_iteration()

		elif reason == rpm.RPMCALLBACK_UNINST_START:
			TextBuffer.insert_at_cursor("RPMCALLBACK_UNINST_START" + "\n")
			Gtk.main_iteration()

		elif reason == rpm.RPMCALLBACK_UNINST_STOP:
			TextBuffer.insert_at_cursor("RPMCALLBACK_UNINST_STOP" + "\n")
			Gtk.main_iteration()

		elif reason == rpm.RPMCALLBACK_REPACKAGE_PROGRESS:
			TextBuffer.insert_at_cursor("RPMCALLBACK_REPACKAGE_PROGRESS" + "\n")
			Gtk.main_iteration()

		elif reason == rpm.RPMCALLBACK_REPACKAGE_START:
			TextBuffer.insert_at_cursor("RPMCALLBACK_REPACKAGE_START" + "\n")
			Gtk.main_iteration()

		elif reason == rpm.RPMCALLBACK_REPACKAGE_STOP:
			TextBuffer.insert_at_cursor("RPMCALLBACK_REPACKAGE_STOP" + "\n")
			Gtk.main_iteration()

		elif reason == rpm.RPMCALLBACK_UNPACK_ERROR:
			TextBuffer.insert_at_cursor("RPMCALLBACK_UNPACK_ERROR" + "\n")
			Gtk.main_iteration()

		elif reason == rpm.RPMCALLBACK_CPIO_ERROR:
			TextBuffer.insert_at_cursor("RPMCALLBACK_CPIO_ERROR" + "\n")
			Gtk.main_iteration()

		else:
			TextBuffer.insert_at_cursor(str(reason))
			Gtk.main_iteration()

