Class: DownloadProcess

Inherits:
Qt::Process
  • Object
show all
Defined in:
lib/download.rb

Overview


Defined Under Namespace

Classes: Command

Constant Summary collapse

DEBUG_DOWNLOAD =
false
DOWNLOAD =
0
CONVERT =
1
FINISHED =
2
INITIAL =
0
RUNNING =
1
ERROR =
2
DONE =
3

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parent, metaInfo, fName) ⇒ DownloadProcess

Returns a new instance of DownloadProcess.



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/download.rb', line 99

def initialize(parent, metaInfo, fName)
    super(parent)
    @metaInfo = metaInfo
    @parent = parent
    @taskItem = nil
    @startTime = Time.new
    @sourceUrl = @metaInfo.wma.url
    @rawFileName = fName
    @rawFilePath = File.join(IRecSettings.rawDownloadDir, fName)
    mkdirSavePath(@rawFilePath)
    @outFileName = @rawFileName.gsub(/\.\w+$/i, '.mp3')
    @outFilePath = File.join(IRecSettings.downloadDir, @outFileName)
    mkdirSavePath(@outFilePath)
    $log.debug { "@rawFilePath : #{@rawFilePath }" }
    $log.debug { "@outFilePath : #{@outFilePath}" }

    @stage = DOWNLOAD
    @status = INITIAL

    connect(self, SIGNAL('finished(int,QProcess::ExitStatus)'), self, SLOT('taskFinished(int,QProcess::ExitStatus)') )
end

Instance Attribute Details

#fileNameObject (readonly)

Returns the value of attribute fileName.



34
35
36
# File 'lib/download.rb', line 34

def fileName
  @fileName
end

#outFilePathObject (readonly)

Returns the value of attribute outFilePath.



97
98
99
# File 'lib/download.rb', line 97

def outFilePath
  @outFilePath
end

#rawFileNameObject (readonly)

Returns the value of attribute rawFileName.



96
97
98
# File 'lib/download.rb', line 96

def rawFileName
  @rawFileName
end

#rawFilePathObject (readonly)

Returns the value of attribute rawFilePath.



97
98
99
# File 'lib/download.rb', line 97

def rawFilePath
  @rawFilePath
end

#sourceUrlObject (readonly)

Returns the value of attribute sourceUrl.



34
35
36
# File 'lib/download.rb', line 34

def sourceUrl
  @sourceUrl
end

#taskItemObject

Returns the value of attribute taskItem.



35
36
37
# File 'lib/download.rb', line 35

def taskItem
  @taskItem
end

Instance Method Details

#beginTaskObject



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
# File 'lib/download.rb', line 143

def beginTask
    startTask = decideStartTask

    # ask whether proceed or commence from start.
    case startTask
    when FINISHED
        ret = OkCancelDialog.ask(nil, \
            i18n('File %s is already exist. Download it anyway ?') % @outFileName)
        if ret == Qt::Dialog::Accepted then
            startTask = DOWNLOAD
        end
    when CONVERT
        ret = OkCancelDialog.ask(nil, \
            i18n('Raw file %s is already exist. Download it anyway ?') % @rawFileName)
        if ret == Qt::Dialog::Accepted then
            startTask = DOWNLOAD
        end
    end

    # initialize task
    case startTask
    when DOWNLOAD
        beginDownload
    when CONVERT
        beginConvert
    when FINISHED
        allTaskFinished
    end
end

#cancelTaskObject



202
203
204
205
206
207
208
209
210
# File 'lib/download.rb', line 202

def cancelTask
    if running? then
        self.terminate
        self.status = ERROR
        errMsg = "Stopped " + %w{ Download Convert ? }[@stage]
        $log.error { [ errMsg ] }
        passiveMessage(errMsg)
    end
end

#decideConvert?Boolean

Returns:

  • (Boolean)


129
130
131
132
133
134
135
# File 'lib/download.rb', line 129

def decideConvert?
    if File.exist?(@rawFilePath) then
        # check rawFile validity.
        return ! rawFileError?
    end
    return false
end

#decideFinish?Boolean

Returns:

  • (Boolean)


121
122
123
124
125
126
127
# File 'lib/download.rb', line 121

def decideFinish?
    if File.exist?(@outFilePath) then
        # check outFile validity.
        return ! outFileError?
    end
    return false
end

#decideStartTaskObject



137
138
139
140
141
# File 'lib/download.rb', line 137

def decideStartTask
    return FINISHED if decideFinish?
    return CONVERT if decideConvert?
    return DOWNLOAD
end

#error?Boolean

Returns:

  • (Boolean)


73
74
75
# File 'lib/download.rb', line 73

def error?
    @status == ERROR
end

#errorStop(exitCode, exitStatus) ⇒ Object



230
231
232
233
234
235
# File 'lib/download.rb', line 230

def errorStop(exitCode, exitStatus)
    self.status = ERROR
    errMsg = makeErrorMsg
    $log.error { [ errMsg, "exitCode=#{exitCode}, exitStatus=#{exitStatus}" ] }
    passiveMessage(errMsg)
end

#finished?Boolean


check stage

Returns:

  • (Boolean)


79
80
81
# File 'lib/download.rb', line 79

def finished?
    @stage == FINISHED
end

#lapseObject



226
227
228
# File 'lib/download.rb', line 226

def lapse
    Time.now - @startTime
end

#rawDownloaded?Boolean

Returns:

  • (Boolean)


83
84
85
# File 'lib/download.rb', line 83

def rawDownloaded?
    @stage >= CONVERT
end

#removeDataObject



212
213
214
215
216
217
218
219
220
# File 'lib/download.rb', line 212

def removeData
    cancelTask
    begin
        File.delete(@rawFilePath)
        File.delete(@outFilePath)
    rescue => e
        $log.info { e }
    end
end

#retryDownloadObject



191
192
193
194
195
196
197
198
199
200
# File 'lib/download.rb', line 191

def retryDownload
    $log.debug { "retry from download." }
    if error? then
        # retry
        @startTime = Time.new
        beginDownload
    else
        $log.warn { "cannot retry the successfully finished or running process." }
    end
end

#retryTaskObject



174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'lib/download.rb', line 174

def retryTask
    $log.debug { "retry." }
    if error? then
        # retry
        case @stage
        when DOWNLOAD
            @startTime = Time.new
            beginDownload
        when CONVERT
            @startTime = Time.new
            beginConvert
        end
    else
        $log.warn { "cannot retry the successfully finished or running process." }
    end
end

#running?Boolean


check status

Returns:

  • (Boolean)


69
70
71
# File 'lib/download.rb', line 69

def running?
    @status == RUNNING
end

#status=(st) ⇒ Object



61
62
63
64
65
# File 'lib/download.rb', line 61

def status=(st)
    @status = st
    @taskItem.status = statusMessage if @taskItem
    $log.misc { "status:#{status}" }
end

#statusMessageObject



50
51
52
53
54
55
56
57
58
59
# File 'lib/download.rb', line 50

def statusMessage
    case @status
    when INITIAL, RUNNING, DONE
        %w{ Downloading Converting Finished }[@stage]
    when ERROR
        "Error : " + %w{ Download Convert Finish }[@stage]
    else
        "???"
    end
end

#taskFinished(exitCode, exitStatus) ⇒ Object



238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
# File 'lib/download.rb', line 238

def taskFinished(exitCode, exitStatus)
    checkReadOutput
    if error? || ((exitCode.to_i.nonzero? || exitStatus.to_i.nonzero?) && checkErroredStatus) then
        errorStop(exitCode, exitStatus)
    else
        $log.info {
            [ "Successed to download a File '%#2$s'",
                "Successed to convert a File '%#2$s'", "?" ][@stage] %
            [ @sourceUrl, @rawFilePath ]
        }
        if @stage == CONVERT then
            passiveMessage(i18n("Download, Convert Complete. '%#1$s'") % [@outFilePath])
        end
        nextTask
    end
end

#updateLapseObject



222
223
224
# File 'lib/download.rb', line 222

def updateLapse
    taskItem.updateTime(lapse)
end

#updateViewObject



255
256
257
258
259
260
261
262
263
# File 'lib/download.rb', line 255

def updateView
    if running? then
        # update Lapse time
        updateLapse

        # dump IO message buffer
        checkReadOutput
    end
end