home *** CD-ROM | disk | FTP | other *** search
/ PC Welt 2004 March / PCWELT_3_2004.ISO / pcwsoft / flaskmpeg_078_39_src.z.exe / flaskmpeg / Profile.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2002-10-28  |  7.2 KB  |  326 lines

  1. /* 
  2.  *  Profile.cpp 
  3.  *
  4.  *    Copyright (C) Alberto Vigata - July 2000 - ultraflask@yahoo.com
  5.  *
  6.  *  This file is part of FlasKMPEG, a free MPEG to MPEG/AVI converter
  7.  *    
  8.  *  FlasKMPEG is free software; you can redistribute it and/or modify
  9.  *  it under the terms of the GNU General Public License as published by
  10.  *  the Free Software Foundation; either version 2, or (at your option)
  11.  *  any later version.
  12.  *   
  13.  *  FlasKMPEG is distributed in the hope that it will be useful,
  14.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  15.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16.  *  GNU General Public License for more details.
  17.  *   
  18.  *  You should have received a copy of the GNU General Public License
  19.  *  along with GNU Make; see the file COPYING.  If not, write to
  20.  *  the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. 
  21.  *
  22.  */
  23.  
  24.  
  25. #include <windows.h>
  26. #include "Profile.h"
  27. #include "Auxiliary.h"
  28.  
  29. #ifdef _DEBUG
  30. #undef THIS_FILE
  31. static char THIS_FILE[]=__FILE__;
  32. #define new DEBUG_NEW
  33. #endif
  34.  
  35. //////////////////////////////////////////////////////////////////////
  36. // Construction/Destruction
  37. //////////////////////////////////////////////////////////////////////
  38.  
  39. CProfile::CProfile(char *program_directory)
  40. {
  41.     n_selected = 0;
  42.     if(program_directory)
  43.         strcpy(this->program_directory, program_directory);
  44. }
  45.  
  46. CProfile::~CProfile()
  47. {
  48.  
  49. }
  50.  
  51. static char *validateName( char *name )
  52. {
  53.   static char validname[1024];
  54.   strcpy( validname, name );
  55.   validateFileName( validname );
  56.   return validname;
  57. }
  58.  
  59. int CProfile::LoadProfileFromDisk(char *fileName)
  60. {
  61.     FILE *input_profile;
  62.     TProfile *myProfile;
  63.  
  64.     // Open file with this name
  65.     if(!(input_profile=fopen(fileName, "rb")) )
  66.         return 0;
  67.     // create temporary profile
  68.     myProfile = (TProfile *)malloc( sizeof(TProfile) );
  69.  
  70.     // Load profile from disk
  71.     if( fread( myProfile, 1, sizeof(TProfile), input_profile ) != sizeof(TProfile) )
  72.     {
  73.         free(myProfile);
  74.         fclose(input_profile);
  75.         return 0;
  76.     }
  77.     // If the profile version doesnt match with this version, drop it
  78.     if(myProfile->nProfileVersion!=PROFILE_VERSION)
  79.     {
  80.       free(myProfile);
  81.       fclose(input_profile);
  82.       return 0;
  83.     }
  84.     // Add profile to our profile list
  85.     profiles.AddItem( myProfile );
  86.     
  87.     fclose(input_profile);
  88.     free(myProfile);
  89.     return 1;
  90. }
  91.  
  92. // Refresh: Load all available profiles from disk
  93. int CProfile::Refresh()
  94. {
  95.     WIN32_FIND_DATA        find_data;
  96.     char                directory[MAX_PATH], szTemp[MAX_PATH];
  97.     HANDLE                search_handle;
  98.     int                    i;
  99.  
  100.     
  101.   sprintf(directory, "%s\\Profiles\\*.profile.flask", program_directory );
  102.  
  103.   profiles.EmptyArray();
  104.  
  105.     i=0;
  106.     search_handle = FindFirstFile(directory, &find_data);
  107.     if(search_handle==INVALID_HANDLE_VALUE){
  108.         profiles.EmptyArray();
  109.     }
  110.     else{
  111.     sprintf(szTemp,"%s\\Profiles\\%s", program_directory, find_data.cFileName );
  112.         LoadProfileFromDisk( szTemp );
  113.  
  114.         while( FindNextFile(search_handle, &find_data ) ){
  115.             sprintf(szTemp,"%s\\Profiles\\%s", program_directory, find_data.cFileName );
  116.             LoadProfileFromDisk( szTemp );
  117.         }
  118.         FindClose(search_handle);
  119.     }
  120.   
  121.   Sort();
  122.  
  123.   SelectDefault();
  124.  
  125.  
  126.   return profiles.GetCount();
  127. }
  128.  
  129. int CProfile::AddProfile(TProfile *profile_data )
  130. {
  131.     char szTemp[MAX_PATH];
  132.     FILE *file;
  133.  
  134.   if(!profile_data)
  135.       return 0;
  136.  
  137.   profile_data->nProfileVersion = PROFILE_VERSION;
  138.  
  139.     strcpy(szTemp, program_directory);
  140.     strcat(szTemp,"\\Profiles" );
  141.     //Creating Profiles folder
  142.     CreateDirectory(szTemp, NULL);
  143.  
  144.     sprintf(szTemp, "%s\\%s.profile.flask", szTemp, validateName(profile_data->profile_name) );
  145.     if(!(file=fopen(szTemp, "wb"))){
  146.         //Ooops
  147.         return 0;
  148.     }
  149.     else
  150.     {
  151.         fwrite( profile_data, sizeof(TProfile), 1, file);
  152.         fclose(file);
  153.         // Now, reload profiles
  154.         return Refresh();
  155.     }
  156. }
  157.  
  158. int CProfile::DeleteProfile(int profileIndex)
  159. {
  160.     char szTemp[MAX_PATH];
  161.  
  162.     // Cant delete profile 0
  163.     if( profileIndex <=0 || profileIndex > (profiles.GetCount()-1) )
  164.         return NULL;
  165.  
  166.     sprintf(szTemp, "%s\\Profiles\\%s.profile.flask", program_directory, validateName(profiles[profileIndex].profile_name) );
  167.   DeleteFile(szTemp);
  168.   return Refresh();
  169. }
  170.  
  171. int CProfile::GetCount()
  172. {
  173.     return profiles.GetCount();
  174. }
  175.     
  176. TProfile * CProfile::Get( int profileIndex)
  177. {
  178.     if( profileIndex <0 || profileIndex > (profiles.GetCount()-1) )
  179.         return NULL;
  180.     return &profiles[profileIndex];
  181. }
  182.  
  183. TProfile * CProfile::GetSelected()
  184. {
  185.     return &profiles[n_selected];
  186. }
  187.  
  188. int CProfile::GetSelectedIndex()
  189. {
  190.     return n_selected;
  191. }
  192.  
  193. int CProfile::Select(int profileIndex)
  194. {
  195.     if( profileIndex <0 || profileIndex > (profiles.GetCount()-1) )
  196.         return NULL;
  197.     n_selected = profileIndex;
  198.     return 1;
  199. }
  200.  
  201. bool CProfile::IsDefault()
  202. {
  203.   return strcmp(profiles[n_selected].profile_name, "Default")==0;
  204. }
  205.  
  206. // Sets passed profiled as default
  207. bool CProfile::SetDefault(TProfile *prof)
  208. {
  209.   // Change the name of the profile to "Default"
  210.   strcpy( prof->profile_name, "Default");
  211.   
  212.   DeleteProfile(GetDefaultIndex());
  213.   AddProfile(prof);
  214.  
  215.   return true;
  216. }
  217.  
  218. bool CProfile::IsSameProfile(TProfile *prof)
  219. {
  220.   return memcmp( prof, GetSelected(), sizeof(TProfile) ) == 0;
  221. }
  222.  
  223. TProfile *CProfile::GetDefault()
  224. {
  225.   for( int i=0; i<profiles.GetCount(); i++ )
  226.   {
  227.     if( strcmp(Get(i)->profile_name, "Default")==0 )
  228.       break;
  229.   }
  230.   if( i >= profiles.GetCount() )
  231.     i--;
  232.   return Get(i);
  233. }
  234.  
  235. int CProfile::GetDefaultIndex()
  236. {
  237.   for( int i=0; i<profiles.GetCount(); i++ )
  238.   {
  239.     if( strcmp(Get(i)->profile_name, "Default")==0 )
  240.       break;
  241.   }
  242.   if( i >= profiles.GetCount() )
  243.     i--;
  244.   return i;
  245. }
  246.  
  247. void CProfile::Sort()
  248. {
  249.   // First take the default and put it at the beginning
  250.   Swap(GetDefaultIndex(), 0);
  251.  
  252.   // Use stupid and simple bubble algorithm
  253.   int max, nItems = profiles.GetCount();
  254.   while( nItems > 1 ) // Dont include 0. The default and 1
  255.   {
  256.     max = 1;
  257.     for(int i=1; i<nItems; i++)
  258.     {
  259.       // if profile at i is greater than the current max
  260.       if(_stricmp(profiles[i].profile_name, profiles[max].profile_name)>0)
  261.         max = i;
  262.     }
  263.     // Swap the maximum to the last
  264.     Swap(nItems-1, max);
  265.     nItems--;
  266.   }
  267. }
  268.  
  269. void CProfile::Swap(int a, int b)
  270. {
  271.   int nItems = profiles.GetCount();
  272.   // Check the limits of a and b
  273.   if( a>=nItems || b>=nItems )
  274.     return;
  275.  
  276.   TProfile sTempProf;
  277.  
  278.   sTempProf = profiles[a];
  279.   profiles[a] = profiles[b];
  280.   profiles[b] = sTempProf;
  281.  
  282. }
  283. void CProfile::SelectDefault()
  284. {
  285.   Select( GetDefaultIndex() );
  286. }
  287.  
  288. bool CProfile::AddNSelect( TProfile *prof )
  289. {
  290.   if( !prof )
  291.     return false;
  292.  
  293.   // If the current profile is different from the selected one
  294.   if ( IsSameProfile( prof ) == false )
  295.   {
  296.     // Set as default 
  297.     SetDefault( prof );
  298.     // And select it
  299.     SelectDefault();
  300.   }
  301.   return true;
  302. }
  303.  
  304. int CProfile::FindIndexWithName(char *profile_name)
  305.  
  306. {
  307.  
  308.   for( int i=0; i<profiles.GetCount(); i++ )
  309.  
  310.   {
  311.  
  312.     if( strcmp(Get(i)->profile_name, profile_name)==0 )
  313.  
  314.       break;
  315.  
  316.   }
  317.  
  318.   if( i >= profiles.GetCount() )
  319.  
  320.     return GetDefaultIndex();
  321.  
  322.   else
  323.  
  324.     return i;
  325.  
  326. }