Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/printscript.py
blob: 303aed2c75c3861c915a8f458ce24b135c2adc39 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
#!/usr/bin/env python
import gtk 
import gtkunixprint
import logging
import threading
import cups
import sys
#import cupspk
from gettext import gettext as _
import gobject
import gtk
import os
import tempfile
import time
import pygtk
_logger = logging.getLogger('print-activity:printscript')


class PageSetup(gtk.Dialog):

    def __init__(self, title=None,):
        printDialog = gtkunixprint.PageSetupUnixDialog('Page Setup',
                                                        gtk.Window())
        printDialog.show_all()
        printDialog.connect('response',self.processResponse)
   
    def processResponse(self, widget, settings):
        _PAGE_SETUP = widget.get_page_setup()
        widget.destroy()
        return _PAGE_SETUP       
    

class PrinterSetup(gtk.Dialog):
   
    def __init__(self,title, filepath):
        printDialog = gtkunixprint.PrintUnixDialog('Printer Settings',
                                                   gtk.Window())       
        printDialog.show_all()
        printDialog.connect('response',self.processResponse,title,filepath)
        
    def processResponse(self, widget, settings,title,filepath):
        _PRINT_SETTINGS = widget.get_settings()
        _PRINTER = widget.get_selected_printer()
        print _PRINTER.get_name()
        widget.destroy()
        pgSetup = self.getDefaultPageLayout()
        
        if settings == -5:
           PrintJobObj = PrintJob(title, _PRINTER, 
                                  _PRINT_SETTINGS,
                                  pgSetup, filepath)
   
    def getDefaultPageLayout(self,):
        pgSetup = gtk.PageSetup()
        return pgSetup
       
        
           
        
class PrintJob:
    def __init__(self, title=None,
                 printer=None, 
                 settings=None, 
                 page_setup=None,
                 file_path=None):

        printJob = gtkunixprint.PrintJob(title,printer,
                                         settings, page_setup)         
        printJob.set_source_file(file_path)
        printJob.send(self.printDone)

    def printDone(self, arg1, arg2, arg3):
        pass

class serverConnection:
    
    def __init__ (self, host=None, port=None, encryption=None, lock=False):
        
        if host != None:
            cups.setServer (host)
        if port != None:
            cups.setPort (port)
        if encryption != None:
            cups.setEncryption (encryption)    
  

        self._use_password = ''
        self._use_user = cups.getUser ()
        self._server = cups.getServer ()
        self._port = cups.getPort()
        self._encryption = cups.getEncryption ()
        if self._connect () == 1:
           sys.exit()  
        self._prompt_allowed = True
        self._operation_stack = []
        self._lock = lock
        self._gui_event = threading.Event ()
        self.jobid = []
        #print self._server

    def _connect (self,):
        cups.setUser (self._use_user)
        create_object = cups.Connection
        try:
            self._connection = create_object (host=self._server,
                                                port=self._port,
                                                encryption=self._encryption)
        except RuntimeError:
            _logger.debug("check if cups server is functioning properly")
           # print ('hey i messed up')
            return 1  

    def _getTitle(self,_filepath):
          title = os.path.split(_filepath)[1].rsplit('.',1)[0]
          return title
  
    def _getPrintersList (self,):
        self._dictPrinters = self._connection.getPrinters()
        _thePrinters = self._dictPrinters.keys()
        return _thePrinters

    def _getPrinterURI (self, name):
        self._dictPrinters = self._connection.getPrinters ()
        _printerURI = self._dictPrinters[name]['device-uri']
        return _printerURI
        
    def _getDefaultPrinter (self,):
        self._defaultPrinter = self._connection.getDefault ()
        return self._defaultPrinter

    def _setPrintDevice (self, device = None, uri = None ):
        if device != None:
            self._ourDevice = device
        else: 
            self._ourDevice = _getDefaultPrinter () 
                 
        if uri != None:
            self._ourURI = _getPrinterURI (self._ourDevice)
        
        setPrinterDevice (self, self._ourDevice, self._ourURI)   
        
  
    def _printPDF (self, _printer, _filename, _title = None):
    
        if _title == None:
            _title = self._getTitle(_filename)
          
# we require our script to not look up the editable names of the printing devices,
# but look up the unchangeable URI of the printer, so we do this:        

        theprinters = self._getPrintersList ()
        self._location = ''
        for printer in theprinters:
           if self._getPrinterURI(printer) == u'cups-pdf:/':
               _printer = printer 
               
# Now we have the printer at hand
#lets open the config files and check the paths, and if user Directories are enabled
#Check for the paths, if a default $USER path exists, skip the condition and just use
#that if not go to user.dir, and also, that particular definition does not exist, use
#home dir, as it defaults to that 

              # print _printer 
               f = open('/etc/cups/cups-pdf.conf', 'r')
               for line in f:
                   if line[0:3] == 'Out':
                       folder = self._location = line[4:].rsplit('\n',1)[0]
                       folder = folder.strip(' $ { } ')
                       
                       if self._location.find('${USER}') == -1:           #Special Fedora case
                           desktop = os.getenv("HOME")+'/.config/user-dirs.dirs'
                           #print desktop
                           try:
                               f1 = open(desktop, 'r')
                           except IOError:

                                _logger.debug("$DESKTOP isn't defined, so going to home directory")
                                self._location = os.getenv("HOME") 
                                f1.close() 
                           else:
                               self._location = os.getenv("HOME") 
                               for line in f1:
                                  # print line[0:15]     
                                   if line[0:15] == 'XDG_'+folder+'_DIR':
                                       self._location = line[15:].rsplit('\n',1)[0].strip('= "')
                                       self._location = self._location.replace('$HOME',os.getenv("HOME"))
                                       break     
                                         
                                          
                               f1.close()
                               #print self._location 
                                 
                       else:        
                           self._location = self._location.replace('${USER}', os.getenv("USER"))
                       break #break to end 2nd for loop

           #print _printer 
           #print _filename
           #print _title
           self._connection.printFile (_printer, _filename, _title, {})
           break            # break to end 1st for loop
        return self._location+'/'+_title+'.pdf'

    def _printtoPrinter (self,_filename, _title=None):
        if _title == None:
            _title = self._getTitle(_filename)
        printer = self._getDefaultPrinter()
        if self._getPrinterURI(printer) != u'cups-pdf:/':
            self._connection.printFile (printer, _filename, _title, {})
            return True
        return False
    def _selectDefaultPrinter (self,_printer):
        self._connection.setDefault(_printer)

    def _getLiveJobQueue (self,noOfJobs=None):
        if noOfJobs == None:
            limit = -1
        else:
            limit = noOfJobs
        return self._connection.getJobs('not-completed', True, limit, -1)
    
    def _getCompletedJobQueue (self, noOfJobs = None):
        if noOfJobs == None:
            limit = -1
        else:
            limit = noOfJobs
        return self._connection.getJobs('completed', True, limit, -1)
           
    def _clearJobQueue (self,):
        printers = self._getPrintersList()
        for printer in printers:
            printeruri = self._getPrinterURI(printer)
            self._connection.cancelAllJobs(printeruri,False,True)
        return True

    def _clearPrinterQueue(self,printer):
        printeruri = self._getPrinterURI(printer)
        self._connection.cancelAllJobs(printeruri,False,True)

    def _cancelAJob(self,jobid):
       self._connection.cancelJob(jobid)
       return True
   












#hello = serverConnection()
#lol = hello._printPDF('Cups-PDF','/home/iwikiwi/lolmax.odt','lolmax')
#print '\''+ lol + '\''