~ubuntu-branches/ubuntu/gutsy/oggconvert/gutsy

« back to all changes in this revision

Viewing changes to OggConvert/ocv_util.py

  • Committer: Bazaar Package Importer
  • Author(s): Luca Falavigna
  • Date: 2007-06-22 23:29:16 UTC
  • Revision ID: james.westby@ubuntu.com-20070622232916-l4c1of5kqn8mykyc
Tags: upstream-0.2.0
ImportĀ upstreamĀ versionĀ 0.2.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/python
 
2
#
 
3
#
 
4
# OggConvert -- Converts media files to Free formats
 
5
# (c) 2007 Tristan Brindle <tcbrindle at gmail dot com>
 
6
#
 
7
# This program is free software; you can redistribute it and/or
 
8
# modify it under the terms of the GNU Lesser General Public
 
9
# License as published by the Free Software Foundation; either
 
10
# version 2.1 of the License, or (at your option) any later version.
 
11
 
12
# This program is distributed in the hope that it will be useful,
 
13
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
14
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
15
# Lesser General Public License for more details.
 
16
 
17
# You should have received a copy of the GNU Lesser General Public
 
18
# License along with this program; if not, write to the Free Software
 
19
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 
20
#
 
21
 
 
22
 
 
23
import gtk
 
24
import os.path
 
25
import ocv_info
 
26
 
 
27
def timeremaining(elapsed, percent):
 
28
    """Returns a string with the remaining time for an operation.        
 
29
       elapsed: elapsed time for the operation so far (in seconds)
 
30
       percent: percentage of the operation completed so far"""
 
31
       
 
32
    if percent == 0:
 
33
        return "unknown time"
 
34
    else:
 
35
        secs_rem = int((100-percent) * elapsed/float(percent))
 
36
        time_rem = hourminsec(secs_rem)
 
37
        #I'm sure there are much smarter ways to do this...
 
38
        if time_rem[0] == 1:
 
39
            h_string = "1 hour"
 
40
        else:
 
41
            h_string = "%i hours" %time_rem[0]
 
42
            
 
43
        if time_rem[1] == 1:
 
44
            m_string = "1 minute"
 
45
        else:
 
46
            m_string = "%i minutes" %time_rem[1]
 
47
        
 
48
        if time_rem[2] == 2:
 
49
            s_string = "1 second"
 
50
        else:
 
51
            s_string = "%i seconds" %time_rem[2]
 
52
        
 
53
        if secs_rem > 3600:
 
54
            return "%s %s" %(h_string, m_string)
 
55
        elif secs_rem > 180:
 
56
            return "%s" %(m_string)
 
57
        elif secs_rem > 59:
 
58
            return "%s %s" %(m_string, s_string)
 
59
        else:
 
60
            return "%s" %(s_string)
 
61
            
 
62
 
 
63
def hourminsec(time):
 
64
    """Converts a given time in seconds to an (hours,minutes,seconds) tuple"""
 
65
    seconds = time
 
66
    minutes = seconds // 60
 
67
    seconds = seconds % 60
 
68
    hours = minutes //60
 
69
    minutes = minutes % 60      
 
70
    return (hours, minutes, seconds)
 
71
        
 
72
def confirm_overwrite(path, window=None):
 
73
    """
 
74
    Displays a dialogue asking the user to confirm they wish to overwrite the
 
75
    file given in path. Return True if they wish to overwrite, False otherwise.
 
76
    The option argument window specifies a GtkWindow to use as a transient.
 
77
    (And yes, the text is copied word-for-word from Nautilus...)
 
78
    """
 
79
    dialogue = gtk.MessageDialog(window, gtk.DIALOG_MODAL, gtk.MESSAGE_WARNING,
 
80
     gtk.BUTTONS_NONE,
 
81
      "A file named \"%s\" already exists. Do you want to replace it?" %os.path.basename(path))
 
82
    
 
83
    dirname = os.path.basename(os.path.dirname(path)) # Urgh!
 
84
    dialogue.format_secondary_text("The file already exists in \"%s\". Replacing it will overwrite its contents." %dirname)
 
85
    
 
86
    dialogue.add_buttons(gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL, "_Replace", gtk.RESPONSE_OK)
 
87
    response = dialogue.run()
 
88
    dialogue.destroy()
 
89
    if response ==gtk.RESPONSE_OK: return True
 
90
    else: return False
 
91
    
 
92
def dirac_warning(window=None):
 
93
    """
 
94
    Displays a warning box asking the user to make sure they realise Dirac is
 
95
    experimental.
 
96
    Returns True if the user chooses to continue, False otherwise
 
97
    """
 
98
    
 
99
    dialogue = gtk.MessageDialog(window, gtk.DIALOG_MODAL, gtk.MESSAGE_WARNING,
 
100
                 gtk.BUTTONS_NONE, "Dirac encoding still experimental")
 
101
                 
 
102
    dialogue.format_secondary_text("The Dirac encoder is still experimental. \
 
103
Files you convert with this version may not be viewable with future versions \
 
104
of the decoder.")
 
105
 
 
106
    dialogue.add_buttons(gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL, "Contin_ue", gtk.RESPONSE_OK)
 
107
    response = dialogue.run()
 
108
    dialogue.destroy()
 
109
    if response==gtk.RESPONSE_OK: return True
 
110
    else: return False
 
111
    
 
112
def stall_warning(window=None):
 
113
    """
 
114
    Displays a box warning the user that encoding has stalled, and they should
 
115
    quit
 
116
    """
 
117
    
 
118
    dialogue = gtk.MessageDialog(window, gtk.DIALOG_MODAL, gtk.MESSAGE_WARNING,
 
119
                gtk.BUTTONS_CLOSE, "Encoding seems to have stalled")
 
120
    
 
121
    # Three sentences. Too much?
 
122
    dialogue.format_secondary_text("If encoding was nearly complete, then it probably finished successfully. At other times, it means there has was problem with the encoder. In either case, it is recommended you cancel the encoding process and check the output file.")
 
123
    dialogue.run()
 
124
    dialogue.destroy()
 
125
    
 
126
def cancel_check(window=None):
 
127
    """
 
128
    Pops up a dialogue box asking if the user is sure they want to stop encoding
 
129
    Returns True if to stop, False otherwise
 
130
    """
 
131
    dialogue = gtk.MessageDialog(window, gtk.DIALOG_MODAL, gtk.MESSAGE_QUESTION,
 
132
                gtk.BUTTONS_NONE, "Encoding is not complete")
 
133
    
 
134
    dialogue.format_secondary_text("Are you sure you wish to cancel?")
 
135
    
 
136
    dialogue.add_buttons(gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL, "Contin_ue", gtk.RESPONSE_OK)
 
137
    
 
138
    response = dialogue.run()
 
139
    dialogue.destroy()
 
140
    if response==gtk.RESPONSE_CANCEL: return True
 
141
    else: return False
 
142
    
 
143
def about_dialogue(window=None):
 
144
    """
 
145
    Pops up a standard GTK About dialogue. Grabs all the info from ocv_info
 
146
    """
 
147
    dialogue = gtk.AboutDialog()
 
148
    dialogue.set_transient_for(window)
 
149
    
 
150
    dialogue.set_name("OggConvert")
 
151
    dialogue.set_authors(ocv_info.authors)
 
152
    dialogue.set_version(ocv_info.version)
 
153
    dialogue.set_copyright(ocv_info.copyright)
 
154
    dialogue.set_website(ocv_info.website)
 
155
    dialogue.set_license(ocv_info.licence) # Learn to spell, GTK!
 
156
    
 
157
    dialogue.run()
 
158
    dialogue.destroy()
 
159