Projects/ds4clonerestore/ds4clonerestore/src/utility.cpp

00001 /******************************************************
00002 * DS4 Clone-Restore
00003 *
00004 * DS4 Laser Technology srl
00005 *
00006 * author: Carsana Francesco <francescocarsana@ds4.it>
00007 *******************************************************/
00008 
00009 #include "utility.h"
00010 
00011 
00012 void CenterWindow(QWidget *window)
00013 {
00014     QDesktopWidget *qdw;
00015     QRect qr;
00016     int width, height;
00017 
00018     qdw = new QDesktopWidget(); // oggetto per ricavare la dimensione del desktop
00019     qr = qdw->screenGeometry(window);
00020     delete qdw;
00021     // ricavo la dimensione del desktop
00022     width = qr.width(); // larghezza
00023     height = qr.height(); // altezza
00024     // centro la finestra
00025     window->move(((width - window->width()) / 2),((height - window->height()) / 2));
00026 }
00027 
00028 
00029 void CenterWindow(QWidget *sourceWindow, QWidget *newWindow)
00030 {
00031     int widthSource,heightSource,width,height,xSource,ySource;
00032 
00033     // centro la finestra rispetto alla finestra sorgente
00034     // la finestra deve essere pi piccola di quella sorgente
00035     widthSource = sourceWindow->width(); // larghezza finestra sorgente
00036     heightSource = sourceWindow->height(); // altezza finestra sorgente
00037     // coordinate angolo in alto a sinistra della finestra sorgente
00038     xSource = sourceWindow->x();
00039     ySource = sourceWindow->y();
00040 
00041     width = newWindow->width(); // larghezza nuova finestra
00042     height = newWindow->height(); // altezza nuova finestra
00043     // sposto la nuova finestra al centro della finestra sorgente
00044     newWindow->move((xSource + ((widthSource - width) / 2)),(ySource + ((heightSource - height) / 2)));
00045 }
00046 
00047 
00048 bool MountRev()
00049 {
00050     char device[20],path[20];
00051 
00052     strcpy(device,RevDevice().ascii());
00053     strcpy(path,RevPathMount().ascii());
00054     
00055     if (mount(device,path,"udf",0,"") == 0) // montato correttamente
00056     {
00057         struct mntent mnt_entry; // elemento di mtab
00058         FILE *fp;
00059 
00060         // riempio i campi dell'elemento mtab
00061         mnt_entry.mnt_fsname = device; // device name
00062         mnt_entry.mnt_dir = path; // mount point
00063         mnt_entry.mnt_type = (char *) "udf"; // tipo filesystem
00064         mnt_entry.mnt_opts = (char *) "defaults,rw,users,noauto"; // opzioni di mount
00065         mnt_entry.mnt_freq = 0; // frequenza di dump
00066         mnt_entry.mnt_passno = 0; // ordine fsck
00067 
00068         fp = setmntent("/etc/mtab","r+"); // apro mtab
00069         addmntent(fp,&mnt_entry); // aggiungo la entry di rev montato
00070         endmntent(fp); // chiudo mtab
00071 
00072         return TRUE;
00073     }
00074     else
00075     {
00076         if (errno == EBUSY) // occupato, già montato
00077         {
00078             return TRUE;
00079         }
00080         else // altro tipo di errore, Rev non montato
00081         {
00082             return FALSE;
00083         }
00084     }
00085 }
00086 
00087 void delmntent(FILE *stream, const char *mount_point)
00088 {
00089     FILE *ftemp; // file di appoggio per modificare mtab
00090     
00091     // elementi di una riga di mtab
00092     char fsname[80],dir[80],type[10],opts[80]; // mount point, device, tipo, opzioni
00093     char freq,passno; // frequenza dump, ordine fsck
00094     
00095     ftemp = tmpfile(); // creo il file temporaneo
00096     
00097     // copio mtab sul file temporaneo
00098     while(!feof(stream))
00099     {
00100         char c;
00101         if ((c = fgetc(stream)) != EOF)
00102             fputc(c,ftemp);
00103     }
00104     
00105     fflush(ftemp); // svuoto il buffer del file temporaneo
00106     rewind(ftemp); // vado all'inizio del file temporaneo
00107 
00108     fclose(stream); // chiudo mtab
00109     stream = fopen("/etc/mtab","w"); // cancello il contenuto di mtab
00110 
00111     // riscrivo mtab saltando l'elemento da eliminare
00112     while(!feof(ftemp)) 
00113     {
00114         // leggo una linea dal buffer
00115         fscanf(ftemp,"%s %s %s %s %c %c",fsname,dir,type,opts,&freq,&passno);
00116         // se l'elemento corrente non è quello da eliminare
00117         if (strcmp(fsname,mount_point) != 0 && strcmp(fsname,"\0") != 0) 
00118         {
00119             //scrivo l'elemento su mtab
00120             fprintf(stream,"%s %s %s %s %c %c\n",fsname,dir,type,opts,freq,passno);
00121             printf("%s %s %s %s %c %c\n",fsname,dir,type,opts,freq,passno);
00122         }
00123         strcpy(fsname,"\0"); // cancello il mount point
00124     }
00125     fclose(ftemp); // chiudo il file temporaneo (viene eliminato automaticamente)
00126 }
00127 
00128 bool UmountRev()
00129 {
00130     char device[20],path[20];
00131     
00132     strcpy(device,RevDevice().ascii());
00133     strcpy(path,RevPathMount().ascii());
00134     
00135     if (umount(path) == 0) // smontato correttamente
00136     {
00137         FILE *fp;
00138 
00139         fp = setmntent("/etc/mtab","r+"); // apro mtab
00140         delmntent(fp,device); // elimino la entry del rev da mtab
00141         endmntent(fp); // chiudo mtab
00142 
00143         return TRUE;
00144     }
00145     else
00146     {
00147         return FALSE;
00148     }
00149 }
00150 
00151 void EjectRev()
00152 {
00153     QProcess proc;
00154     char device[20];
00155     
00156     strcpy(device,RevDevice().ascii());    
00157     
00158     proc.addArgument("eject"); // comando
00159     proc.addArgument(device); // parametro
00160     proc.start(); // eseguo il comando
00161 }
00162 
00163 QString RevPathMount()
00164 {
00165     QSettings settings; // file di configurazione
00166     QString value; // valore del parametro letto dal file
00167 
00168     // leggo il valore del parametro dal file
00169     value = settings.readEntry("DS4CloneRestore/Current/RevMountPoint","/mnt/rev/");
00170 
00171     return value;
00172 }
00173 
00174 QString RevDevice()
00175 {
00176     QSettings settings; // file di configurazione
00177     QString value; // valore del parametro letto dal file
00178 
00179     // leggo il valore del parametro dal file
00180     value = settings.readEntry("DS4CloneRestore/Current/RevDevice","/dev/hdb");
00181 
00182     return value;
00183 }
00184 
00185 
00186 QString HDDevice()
00187 {
00188     QSettings settings; // file di configurazione
00189     QString value; // valore del parametro letto dal file
00190 
00191     // leggo il valore del parametro dal file
00192     value = settings.readEntry("DS4CloneRestore/Current/HdDevice","hda");
00193 
00194     return value;
00195 }
00196 
00197 QString SystemSwapPartition()
00198 {
00199     QSettings settings; // file di configurazione
00200     QString value; // valore del parametro letto dal file
00201 
00202     // leggo il valore del parametro dal file
00203     value = settings.readEntry("DS4CloneRestore/Current/SystemSwapPartition","/dev/sda2");
00204 
00205     return value;
00206 }
00207 
00208 
00209 bool MountServer()
00210 {
00211     FILE *fp; // puntatore a mtab
00212     // elementi di una riga di mtab
00213     char fsname[80],dir[80],type[10],opts[80]; // mount point, device, tipo, opzioni
00214     char freq,passno; // frequenza dump, ordine fsck
00215     QString command; // comando per il mount
00216     bool mounted;
00217     
00218     command = "mount " + ServerAddress() + " " + ServerPathMount() + " -t smbfs -o ro,username=\'" +
00219               ServerUsername() + "\',password=\'" + ServerPassword() + "\'";
00220 
00221     system(command.ascii()); // eseguo il mount
00222     
00223     // Controllo l'esito del mount: se è presente una voce in mtab allora è montato
00224     
00225     fp = setmntent("/etc/mtab","r"); // apro mtab
00226     
00227     mounted = FALSE;
00228     
00229     while(!feof(fp)) // scorro tutte le voci di mtab
00230     {
00231         fscanf(fp,"%s %s %s %s %c %c",fsname,dir,type,opts,&freq,&passno);
00232         if (strcmp(fsname,ServerAddress().ascii()) == 0) // directory remota montata
00233             mounted = TRUE;
00234     }
00235     
00236     endmntent(fp); // chiudo mtab
00237     
00238     return mounted;
00239 }
00240 
00241 bool UmountServer()
00242 {
00243     QString command;
00244     
00245     command = "umount " + ServerPathMount();
00246     
00247     system(command.ascii());
00248     return TRUE;
00249 }
00250 
00251 QString ServerPathMount()
00252 {
00253     QSettings settings; // file di configurazione
00254     QString value; // valore del parametro letto dal file
00255 
00256     // leggo il valore del parametro dal file
00257     value = settings.readEntry("DS4CloneRestore/Current/ServerMountPoint","/mnt/server/");
00258 
00259     return value;
00260 }
00261 
00262 QString ServerAddress()
00263 {
00264     QSettings settings; // file di configurazione
00265     QString value; // valore del parametro letto dal file
00266 
00267     // leggo il valore del parametro dal file
00268     value = settings.readEntry("DS4CloneRestore/Current/ServerAddress","//192.168.3.2/partimage");
00269 
00270     return value;
00271 }
00272 
00273 QString ServerUsername()
00274 {
00275     QSettings settings; // file di configurazione
00276     QString value; // valore del parametro letto dal file
00277 
00278     // leggo il valore del parametro dal file
00279     value = settings.readEntry("DS4CloneRestore/Current/ServerUsername","");
00280 
00281     return value;
00282 }
00283 
00284 QString ServerPassword()
00285 {
00286     QSettings settings; // file di configurazione
00287     QString value; // valore del parametro letto dal file
00288 
00289     // leggo il valore del parametro dal file
00290     value = settings.readEntry("DS4CloneRestore/Current/ServerPassword","");
00291 
00292     return value;
00293 }

Generato il Wed Jun 13 18:59:37 2007 per DS4CloneRestore da  doxygen 1.5.2