00001
00002
00003
00004
00005
00006 #include "stdafx.h"
00007 #include "FdmApp.h"
00008 #include "vmsMediaConvertMgr.h"
00009 #include "WaitDlg.h"
00010 #include "vmsMediaConverter.h"
00011 #include "Dlg_Convert.h"
00012 #include "mfchelp.h"
00013 #include "MyMessageBox.h"
00014
00015 #ifdef _DEBUG
00016 #undef THIS_FILE
00017 static char THIS_FILE[]=__FILE__;
00018 #define new DEBUG_NEW
00019 #endif
00020
00021 vmsMediaConvertMgr::vmsMediaConvertMgr()
00022 {
00023
00024 }
00025
00026 vmsMediaConvertMgr::~vmsMediaConvertMgr()
00027 {
00028
00029 }
00030
00031 void vmsMediaConvertMgr::AddTask(vmsDownloadSmartPtr dld, const vmsMediaFileConvertSettings &stgs)
00032 {
00033 vmsConvertMediaFileContext ctx;
00034 ctx.dld = dld;
00035 ctx.stgs = stgs;
00036 m_vTasks.push_back (ctx);
00037 }
00038
00039 BOOL vmsMediaConvertMgr::SaveState()
00040 {
00041 CString strFile = fsGetDataFilePath ("mctasks.sav");
00042 HANDLE hFile = CreateFile (strFile, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS,
00043 FILE_ATTRIBUTE_HIDDEN, NULL);
00044 if (hFile == INVALID_HANDLE_VALUE)
00045 return FALSE;
00046
00047 DWORD dw;
00048
00049 fsMcMgrFileHdr hdr;
00050 if (FALSE == WriteFile (hFile, &hdr, sizeof (hdr), &dw, NULL))
00051 goto _lErr;
00052
00053 size_t i; i = m_vTasks.size ();
00054 if (FALSE == WriteFile (hFile, &i, sizeof (i), &dw, NULL))
00055 goto _lErr;
00056
00057 for (i = 0; i < m_vTasks.size (); i++)
00058 {
00059 vmsConvertMediaFileContext *ctx = &m_vTasks [i];
00060
00061 if (FALSE == WriteFile (hFile, &ctx->dld->nID, sizeof (UINT), &dw, NULL))
00062 goto _lErr;
00063
00064 if (FALSE == fsSaveStrToFile (ctx->stgs.strFormat, hFile))
00065 goto _lErr;
00066
00067 if (FALSE == fsSaveStrToFile (ctx->stgs.strExtension, hFile))
00068 goto _lErr;
00069
00070 if (FALSE == fsSaveStrToFile (ctx->stgs.strAudioCodec, hFile))
00071 goto _lErr;
00072
00073 if (FALSE == fsSaveStrToFile (ctx->stgs.strVideoCodec, hFile))
00074 goto _lErr;
00075
00076 if (FALSE == WriteFile (hFile, &ctx->stgs.nAudioBitrate, sizeof (int), &dw, NULL))
00077 goto _lErr;
00078
00079 if (FALSE == WriteFile (hFile, &ctx->stgs.nVideoBitrate, sizeof (int), &dw, NULL))
00080 goto _lErr;
00081
00082 if (FALSE == WriteFile (hFile, &ctx->stgs.sizeVideo, sizeof (CSize), &dw, NULL))
00083 goto _lErr;
00084 }
00085
00086 CloseHandle (hFile);
00087
00088 return TRUE;
00089
00090 _lErr:
00091 CloseHandle (hFile);
00092 DeleteFile (strFile);
00093 return FALSE;
00094 }
00095
00096 BOOL vmsMediaConvertMgr::LoadState()
00097 {
00098 CString strFile = fsGetDataFilePath ("mctasks.sav");
00099 HANDLE hFile = CreateFile (strFile, GENERIC_READ, 0, NULL, OPEN_EXISTING,
00100 FILE_ATTRIBUTE_HIDDEN, NULL);
00101 if (hFile == INVALID_HANDLE_VALUE)
00102 return GetFileAttributes (strFile) == DWORD (-1);
00103
00104 DWORD dw;
00105
00106 fsMcMgrFileHdr hdr;
00107 if (FALSE == ReadFile (hFile, &hdr, sizeof (hdr), &dw, NULL))
00108 goto _lErr;
00109
00110 if (lstrcmp (hdr.szSig, MCMGRFILE_SIG) || hdr.wVer > MCMGRFILE_CURRENT_VERSION)
00111 goto _lErr;
00112
00113 size_t i;
00114 if (FALSE == ReadFile (hFile, &i, sizeof (i), &dw, NULL))
00115 goto _lErr;
00116
00117 m_vTasks.clear ();
00118
00119 while (i--)
00120 {
00121 vmsConvertMediaFileContext ctx;
00122
00123 UINT nId;
00124 if (FALSE == ReadFile (hFile, &nId, sizeof (UINT), &dw, NULL))
00125 goto _lErr;
00126
00127 if (FALSE == fsReadStrFromFile (&ctx.stgs.strFormat.pszString, hFile))
00128 goto _lErr;
00129
00130 if (FALSE == fsReadStrFromFile (&ctx.stgs.strExtension.pszString, hFile))
00131 goto _lErr;
00132
00133 if (FALSE == fsReadStrFromFile (&ctx.stgs.strAudioCodec.pszString, hFile))
00134 goto _lErr;
00135
00136 if (FALSE == fsReadStrFromFile (&ctx.stgs.strVideoCodec.pszString, hFile))
00137 goto _lErr;
00138
00139 if (FALSE == ReadFile (hFile, &ctx.stgs.nAudioBitrate, sizeof (int), &dw, NULL))
00140 goto _lErr;
00141
00142 if (FALSE == ReadFile (hFile, &ctx.stgs.nVideoBitrate, sizeof (int), &dw, NULL))
00143 goto _lErr;
00144
00145 if (FALSE == ReadFile (hFile, &ctx.stgs.sizeVideo, sizeof (CSize), &dw, NULL))
00146 goto _lErr;
00147
00148 ctx.dld = _DldsMgr.GetDownloadByID (nId);
00149 if (ctx.dld != NULL)
00150 m_vTasks.push_back (ctx);
00151 }
00152
00153 CloseHandle (hFile);
00154
00155 return TRUE;
00156
00157 _lErr:
00158 CloseHandle (hFile);
00159 DeleteFile (strFile);
00160 return FALSE;
00161 }
00162
00163 DWORD WINAPI vmsMediaConvertMgr::_threadConvertMediaFile(LPVOID lp)
00164 {
00165 ProcWaitInfo *info = (ProcWaitInfo*) lp;
00166
00167 vmsConvertMediaFileContext *pcmfc = (vmsConvertMediaFileContext*) info->lpParam1;
00168 BOOL bDontDeleteSourceFile = (BOOL)info->lpParam2;
00169
00170 CString strDst = pcmfc->dld->pMgr->get_OutputFilePathName ();
00171 strDst = strDst.Left (strDst.GetLength () - 4);
00172
00173 for (int i = 0; ; i++)
00174 {
00175 CString str;
00176 if (i)
00177 str.Format ("%s (%d).%s", strDst, i, pcmfc->stgs.strExtension);
00178 else
00179 str.Format ("%s.%s", strDst, pcmfc->stgs.strExtension);
00180
00181 if (GetFileAttributes (str) == DWORD (-1))
00182 {
00183 strDst = str;
00184 break;
00185 }
00186 }
00187
00188 _DldsMgr.AddEvent (pcmfc->dld, LS (L_CONVERTING), EDT_INQUIRY);
00189
00190 if (vmsMediaConverter::ConvertMedia (pcmfc->dld->pMgr->get_OutputFilePathName (),
00191 strDst, pcmfc->stgs.strFormat, pcmfc->stgs.strAudioCodec, -1, pcmfc->stgs.nAudioBitrate, -1,
00192 pcmfc->stgs.strVideoCodec, pcmfc->stgs.nVideoBitrate, -1, pcmfc->stgs.sizeVideo.cx, pcmfc->stgs.sizeVideo.cy,
00193 &info->iProgress, &info->bNeedStop))
00194 {
00195 if (bDontDeleteSourceFile == FALSE)
00196 {
00197 pcmfc->dld->pMgr->DeleteFile ();
00198
00199 fsDownload_Properties *dp = pcmfc->dld->pMgr->GetDownloadMgr ()->GetDP ();
00200 delete [] dp->pszFileName;
00201 dp->pszFileName = new char [strDst.GetLength () + 1];
00202 lstrcpy (dp->pszFileName, strDst);
00203 }
00204 else
00205 {
00206 CString strDst2 = strDst;
00207 strDst2.SetAt (strDst2.GetLength () - 3, 't');
00208 strDst2.SetAt (strDst2.GetLength () - 2, 'm');
00209 strDst2.SetAt (strDst2.GetLength () - 1, 'p');
00210 if (FALSE == MoveFile (strDst, strDst2))
00211 strDst2 = strDst;
00212 CString strFilter;
00213 strFilter.Format ("%s files (*.%s)|*.%s||", pcmfc->stgs.strExtension,
00214 pcmfc->stgs.strExtension, pcmfc->stgs.strExtension);
00215 CFileDialog dlg (FALSE, pcmfc->stgs.strExtension, strDst,
00216 OFN_OVERWRITEPROMPT|OFN_NOCHANGEDIR, strFilter, AfxGetApp ()->m_pMainWnd);
00217 if (IDCANCEL == _DlgMgr.DoModal (&dlg))
00218 {
00219 DeleteFile (strDst2);
00220 }
00221 else
00222 {
00223 MoveFile (strDst2, strDst);
00224
00225 info->bWaitDone = TRUE;
00226
00227 CString strMsg;
00228 strMsg.Format (LS (L_CONVERTED_OK), strDst);
00229 UINT nRet = MyMessageBox (NULL, strMsg, LS (L_DONE), NULL, IDI_QUESTION,
00230 LS (L_LAUNCH), LS (L_OPENFOLDER), "OK");
00231
00232 if (nRet == IDC_BTN1)
00233 {
00234 ShellExecute (0, "open", strDst, NULL, NULL, SW_SHOW);
00235 }
00236 else if (nRet == IDC_BTN2)
00237 {
00238 CString strCmd;
00239 strCmd.Format ("/select,\"%s\"", strDst);
00240 ShellExecute (NULL, "open", "explorer.exe", strCmd, NULL, SW_SHOW);
00241 }
00242 }
00243 }
00244
00245 _DldsMgr.AddEvent (pcmfc->dld, LS (L_DONE), EDT_RESPONSE_S);
00246
00247 if (bDontDeleteSourceFile == FALSE)
00248 _DldsMgr.DownloadStateChanged (pcmfc->dld);
00249 }
00250 else
00251 {
00252 DeleteFile (strDst);
00253 _DldsMgr.AddEvent (pcmfc->dld, LS (L_FAILED), EDT_RESPONSE_E);
00254 info->bWaitDone = TRUE;
00255 CString strMsg;
00256 strMsg.Format (LS (L_CONVERTED_FAILED), strDst);
00257 MessageBox (NULL, strMsg, LS (L_FAILED), MB_ICONERROR);
00258 }
00259
00260 info->bWaitDone = TRUE;
00261 delete pcmfc;
00262
00263 return 0;
00264 }
00265
00266 void vmsMediaConvertMgr::ConvertMediaFile(vmsDownloadSmartPtr dld, vmsMediaConvertMgr_OptionsSource enOs)
00267 {
00268 vmsConvertMediaFileContext *pcmfc = new vmsConvertMediaFileContext;
00269
00270 if (enOs == MCM_OS_SEARCH_IN_MGR)
00271 {
00272 int nIndex = FindDownload (dld);
00273 if (nIndex == -1)
00274 return;
00275
00276 *pcmfc = m_vTasks [nIndex];
00277 m_vTasks.erase (m_vTasks.begin () + nIndex);
00278 }
00279 else
00280 {
00281 pcmfc->dld = dld;
00282 GetDefaultSettings (pcmfc->stgs);
00283
00284 if (enOs == MCM_OS_SHOW_OPTIONS_UI)
00285 {
00286 CDlg_Convert dlg (&pcmfc->stgs);
00287 if (IDOK != _DlgMgr.DoModal (&dlg))
00288 {
00289 delete pcmfc;
00290 return;
00291 }
00292 }
00293 }
00294
00295 CWaitDlg *dlg = new CWaitDlg;
00296 dlg->StartWaiting (LS (L_CONVERTING), _threadConvertMediaFile, TRUE, pcmfc,
00297 (LPVOID)(enOs == MCM_OS_SHOW_OPTIONS_UI), NULL, NULL, NULL, FALSE);
00298 }
00299
00300 int vmsMediaConvertMgr::FindDownload(vmsDownloadSmartPtr dld)
00301 {
00302 for (size_t i = 0; i < m_vTasks.size (); i++)
00303 {
00304 if (m_vTasks [i].dld->nID == dld->nID)
00305 return i;
00306 }
00307
00308 return -1;
00309 }
00310
00311 void vmsMediaConvertMgr::ConvertMediaFile_SetupDefaultOptions()
00312 {
00313 vmsMediaFileConvertSettings stgs;
00314 GetDefaultSettings (stgs);
00315 CDlg_Convert dlg (&stgs);
00316 dlg.m_bCustomizingDefSettings = true;
00317 _DlgMgr.DoModal (&dlg);
00318 }
00319
00320 void vmsMediaConvertMgr::GetDefaultSettings(vmsMediaFileConvertSettings &stgs)
00321 {
00322 stgs.strFormat = _App.Convert_Format ();
00323 stgs.strExtension = _App.Convert_Extension ();
00324 stgs.sizeVideo = _App.Convert_VideoSize ();
00325 stgs.nAudioBitrate = _App.Convert_AudioBitrate ();
00326 stgs.nVideoBitrate = _App.Convert_VideoBitrate ();
00327 stgs.strAudioCodec = _App.Convert_AudioCodec ();
00328 stgs.strVideoCodec = _App.Convert_VideoCodec ();
00329 }
00330
00331 void vmsMediaConvertMgr::SaveSettingsAsDefault(const vmsMediaFileConvertSettings &stgs)
00332 {
00333 _App.Convert_AudioBitrate (stgs.nAudioBitrate);
00334 _App.Convert_Format (stgs.strFormat);
00335 _App.Convert_VideoBitrate (stgs.nVideoBitrate);
00336 _App.Convert_VideoSize (stgs.sizeVideo);
00337 _App.Convert_VideoCodec (stgs.strVideoCodec);
00338 _App.Convert_AudioCodec (stgs.strAudioCodec);
00339 _App.Convert_Extension (stgs.strExtension);
00340 }
00341
00342 BOOL vmsMediaConvertMgr::ShowSettingsUI(vmsMediaFileConvertSettings &stgs)
00343 {
00344 CDlg_Convert dlg (&stgs);
00345 return IDOK == _DlgMgr.DoModal (&dlg);
00346 }