[gyrus/gyrus-python] add file GyrusReport.py



commit c3664ff3a63302904ba9528ed90835d75d35acd9
Author: Alejandro Valdes jimenez <avaldes amvj in utalca cl>
Date:   Fri Jun 12 12:22:06 2009 -0400

    add file GyrusReport.py

 ChangeLog          |    4 +
 src/GyrusReport.py |  178 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 182 insertions(+), 0 deletions(-)
---
diff --git a/ChangeLog b/ChangeLog
index b69d015..e53008a 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,9 @@
 2009-06-12  Alejandro Valdes jimenez  <avaldes gnome org>
 
+	* src/GyrusReport.py: add script.
+
+2009-06-12  Alejandro Valdes jimenez  <avaldes gnome org>
+
 	* src/GyrusAclStore.py:
 	* src/GyrusAclTreeView.py:
 	* src/GyrusDialogFindMailbox.py:
diff --git a/src/GyrusReport.py b/src/GyrusReport.py
new file mode 100644
index 0000000..1746649
--- /dev/null
+++ b/src/GyrusReport.py
@@ -0,0 +1,178 @@
+#!/usr/bin/env python
+#
+#       GyrusReport.py a Class to create reports.
+#
+#       GYRUS -- GNOME Cyrus Administrator. 
+#       
+#       Copyright 2009 Francisco Rojas <frojas alumnos utalca cl>
+#       
+#       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, write to the Free Software
+#       Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
+#       MA 02110-1301, USA.
+
+import pygtk
+pygtk.require('2.0')
+import gtk
+import cairo
+import pango
+import gettext
+import datetime
+import gyrus_admin_mailbox
+import gobject
+from math import ceil
+
+from GyrusAclTreeView import GyrusAclTreeView
+
+from gyrus_constant import *
+
+gettext.bindtextdomain(APPNAME,DIRLOCALE)
+gettext.textdomain(APPNAME)
+_ = gettext.gettext
+
+gtk.glade.bindtextdomain(APPNAME,DIRLOCALE)
+gtk.glade.textdomain(APPNAME)
+
+#Macros
+MAX_USER_PAGE = 60
+SPACE_PER_LINE = 4
+PAGE_POS_COLUMN_2 = 40
+PAGE_POS_COLUMN_3 = 80
+PAGE_POS_COLUMN_4 = 120
+PAGE_POS_COLUMN_5 = 150
+
+
+class GyrusReport:
+    def __init__(self, admin):
+        self.admin = admin
+        self.settings = gtk.PrintSettings()
+        self.op = gtk.PrintOperation()
+        self.n_page = 0
+
+        # signals
+
+        self.op.connect('begin_print', self.begin_print, self.admin)
+        self.op.connect('draw_page', self.draw_page, self.admin)
+
+        self.do_print()
+
+    def do_print(self):
+        # set some values
+        self.op.set_print_settings(self.settings)
+        self.op.set_unit(gtk.UNIT_MM)
+
+        try:
+            response = self.op.run(gtk.PRINT_OPERATION_ACTION_PRINT_DIALOG)
+        except gobject.GError, ex:
+            error_dialog = gtk.MessageDialog(None,
+                     gtk.DIALOG_DESTROY_WITH_PARENT,
+                     gtk._MESSAGE_ERROR,
+                     gtk.BUTTONS_CLOSE,
+                     ("Error printing file:\n%s" % str(ex)))
+            error_dialog.connect("response", gtk.Widget.destroy)
+            error_dialog.show()
+
+        if response == gtk.PRINT_OPERATION_RESULT_APPLY:
+            settings = self.op.get_print_settings()
+
+
+    def begin_print(self, operation, context, admin):
+        model = admin.treeview_users.get_model()
+        n = float(len(model))
+        self.n_pages =int (ceil (n/MAX_USER_PAGE))
+        operation.set_n_pages(self.n_pages)
+
+    def draw_page(self, operation, context, page,admin):
+        cairo_context = context.get_cairo_context()
+        width = context.get_width()
+        page_setup = context.get_page_setup()
+        left_margin = page_setup.get_left_margin(gtk.UNIT_MM)
+        right_margin = page_setup.get_right_margin(gtk.UNIT_MM)
+        top_margin = page_setup.get_top_margin(gtk.UNIT_MM)
+        bottom_margin = page_setup.get_bottom_margin(gtk.UNIT_MM)
+        page_width = page_setup.get_page_width(gtk.UNIT_MM)
+        page_height = page_setup.get_page_height(gtk.UNIT_MM)
+        x = left_margin
+
+        layout = context.create_pango_layout()
+        desc = pango.FontDescription("Sans 10")
+        layout.set_font_description(desc)
+
+        # print header
+        self.print_header(cairo_context,layout,left_margin,page_width,right_margin,top_margin)
+
+        model = admin.treeview_users.get_model()
+        if(len(model)==0):
+            return
+
+        y = top_margin + (SPACE_PER_LINE * 2)
+
+        index = ( page * MAX_USER_PAGE )
+        end = index + MAX_USER_PAGE
+
+        if end >= len(model):
+            end = len(model)-1
+
+        iter = model.get_iter(index)
+        count = index
+
+        while (iter and count < end):
+            usr = model.get_name_mailbox(iter)
+            free, quota = model.get_quota_of_mailbox(iter)
+
+            self.print_string(cairo_context,layout,x,y,usr)
+            self.print_string(cairo_context,layout,x + PAGE_POS_COLUMN_2,y,str(quota))
+            self.print_string(cairo_context,layout,x + PAGE_POS_COLUMN_3,y,str(quota-free))
+            self.print_string(cairo_context,layout,x + PAGE_POS_COLUMN_4,y,str(free))
+            if (quota == 0):
+                self.print_string(cairo_context,layout,x + PAGE_POS_COLUMN_5,y, str('%     0'))
+            else:
+                self.print_string(cairo_context,layout,x + PAGE_POS_COLUMN_5,y, str('%% %3.0f' % ((free*1.0/quota)*100) ))
+            y = y + SPACE_PER_LINE
+
+            iter = model.iter_next(iter)
+            count = count + 1
+        # print footer
+        self.print_footer(cairo_context,layout,x,page_width,right_margin,page_height,page+1)
+
+
+
+    def print_line(self,cairo_context,width_line,x,width,y):
+        cairo_context.move_to(x,y)
+        cairo_context.line_to(width,y)
+        cairo_context.set_line_join(cairo.LINE_JOIN_ROUND)
+        cairo_context.set_line_width(width_line)
+        cairo_context.stroke()
+
+    def print_string(self,cairo_context,layout,x,y,string):
+        cairo_context.move_to(x,y)
+        layout.set_text(string)
+        cairo_context.show_layout(layout)
+
+    def print_header(self,cairo_context,layout,left_margin,page_width,right_margin,top_margin):
+        self.print_line(cairo_context,WIDTH_LINE,left_margin,page_width - right_margin,top_margin)
+        self.print_string(cairo_context,layout,left_margin,top_margin + WIDTH_LINE,_("Mailbox"))
+        self.print_string(cairo_context,layout,left_margin + PAGE_POS_COLUMN_2,top_margin + WIDTH_LINE,_("Quota assigned"))
+        self.print_string(cairo_context,layout,left_margin + PAGE_POS_COLUMN_3,top_margin + WIDTH_LINE,_("Quota used"))
+        self.print_string(cairo_context,layout,left_margin + PAGE_POS_COLUMN_4,top_margin + WIDTH_LINE,_("Quota free"))
+        self.print_string(cairo_context,layout,left_margin + PAGE_POS_COLUMN_5,top_margin + WIDTH_LINE,_("Percentage"))
+        self.print_line(cairo_context,WIDTH_LINE,left_margin,page_width - right_margin,top_margin + (WIDTH_LINE *2)+ SPACE_PER_LINE)
+
+    def print_footer(self,cairo_context,layout,x,page_width,right_margin,page_height,page):
+        model = self.admin.treeview_users.get_model()
+        now = model.get_time()
+        date = now.strftime(_('%H:%M:%S %m-%d-%Y'))
+        self.print_line(cairo_context,WIDTH_LINE,x,page_width - right_margin,page_height-10)
+        self.print_string(cairo_context,layout,x,page_height - (SPACE_PER_LINE * 2),date)
+        self.print_string(cairo_context,layout,x + PAGE_POS_COLUMN_5, page_height - (SPACE_PER_LINE * 2),_("Page %d" %page))
+



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]