/* EasyPLX beta version, third version Licensed under GNU GPL Copyleft Olle Bergkvist 2007 www.olle.tk e-mail: olle dot bergvist at yahoo dot se GUI tool for making a PLX playlist, So you can access the pictures from the "Pictures" menu in your iriver. */ #include #include #include "config.h" #define MBINFO MB_ICONINFORMATION #define MBERROR MB_ICONERROR #define MBWARNING MB_ICONEXCLAMATION // input char * fileptrs [MAXNUM]; char * listptrs [MAXNUM]; char * entryptrs[MAXNUM]; char filestrs[MAXNUM][MAXLENGTH]; char liststrs[MAXNUM][MAXLENGTH]; char entrystrs[MAXNUM][MAXLENGTH]; char entryfolders[MAXNUM][MAXLENGTH]; char filecount = 0; char listcount = 0; char entrycount = 0; int entrylength[MAXNUM]; // misc. char haserror = 0; char driveletter[1]; // output char outpath[MAXLENGTH]; char outdir[MAXLENGTH]; char outname[MAXLENGTH]; char fileext[MAXLENGTH]; char jfujthdir[MAXLENGTH]; char jfuext[MAXLENGTH]; char jthext[MAXLENGTH]; /* The following is code i had to copy from the Win32 header files to make it work. I dont know what you should do, maybe you have to comment this code away and use the ordinary, maybe you have to do it this way. */ typedef struct _SHITEMID { // mkid USHORT cb; // size of identifier, including cb itself BYTE abID[1]; // variable length item identifier } SHITEMID, * LPSHITEMID; typedef const SHITEMID * LPCSHITEMID; typedef struct _ITEMIDLIST { // idl SHITEMID mkid; // list of item identifers } ITEMIDLIST, * LPITEMIDLIST; typedef const ITEMIDLIST * LPCITEMIDLIST; typedef struct _browseinfo { HWND hwndOwner; // see below LPCITEMIDLIST pidlRoot; // see below LPSTR pszDisplayName; // see below LPCSTR lpszTitle; // see below UINT ulFlags; // see below void * lpfn; // see below LPARAM lParam; // see below int iImage; // see below } BROWSEINFO, *PBROWSEINFO, *LPBROWSEINFO; #define SHBrowseForFolder SHBrowseForFolderW LPITEMIDLIST WINAPI SHBrowseForFolderW(PBROWSEINFO); #define SHGetPathFromIDList SHGetPathFromIDListW BOOL WINAPI SHGetPathFromIDListW(LPCITEMIDLIST,LPWSTR); //End of code copied from the Win32 header files int searchfordrive(HWND hwnd) { int ans=0,pos=0; char ch='C'; long drives=0,exp=4; SetWindowText(hwnd,"Searching for iriver, please wait..."); char cmd[]="?:\\System\\Pictures\\"; pos=0; char driveroot[]="?:\\"; drives=GetLogicalDrives(); for (ch=ch;ch<='Z';ch++){ if(drives & exp){ cmd[pos]=ch; driveroot[0]=ch; if(GetDriveType(driveroot)==2){ if(SetCurrentDirectory(cmd)){ ans=ch; break; } } } exp*=2; } driveletter[0]=ans; if (ans==0) { MessageBox(hwnd,"EasyPLX couldn't find your iriver player! \nEnter the location in the \"Drive Letter\" text box.","EasyPLX",MB_OK|MBWARNING); SetFocus(GetDlgItem(hwnd,editletter)); } SetWindowText(hwnd,"EasyPLX"); SetWindowText(GetDlgItem(hwnd,editletter),driveletter); return ans; } int getpicfilename(HWND hwnd) { OPENFILENAME ofn; char FileName[MAXFILELENGTH]; int returnval=0; char printstr[MAXFILELENGTH]; char folderstr[MAXLENGTH]; int n=0,tmp=0; ZeroMemory(&ofn, sizeof(ofn)); FileName[0] = 0; ofn.lStructSize = sizeof(ofn); ofn.hwndOwner = hwnd; ofn.lpstrFilter = "JPEG Files (*.jpg *.jpeg)\0*.jpg;*.jpeg\0All Files (*.*)\0*.*\0\0"; ofn.lpstrDefExt = "jpg"; ofn.Flags = OFN_EXPLORER | OFN_FILEMUSTEXIST | OFN_HIDEREADONLY | OFN_ALLOWMULTISELECT; ofn.lpstrFile = FileName; ofn.nMaxFile = MAXFILELENGTH; if (GetOpenFileName(&ofn)){ mystrcopy(folderstr, &ofn.lpstrFile[n]); n += mystrlen(&ofn.lpstrFile[n]) + 1 ; //filecount=0; GetWindowText(GetDlgItem(hwnd,editpic),printstr,MAXFILELENGTH); if (ofn.lpstrFile[n]==0){ //the user selected one single file. there is only one string in ofn.lpstrFile // contains both the directory and file name, so there's no need to use folderstr mystrcat(printstr, "\r\nSingle picture: "); mystrcat(printstr, folderstr); filecount++; mystrcopy(filestrs[filecount], folderstr); fileptrs[filecount]=filestrs[filecount]; } else{ if (folderstr[mystrlen(folderstr)-1] != '\\') mystrcat(folderstr,"\\"); mystrcat(printstr,"\r\nFolder: "); mystrcat(printstr, folderstr); } while (ofn.lpstrFile[n]){ mystrcat(printstr, "\r\nPicture: "); mystrcat(printstr, folderstr); mystrcat(printstr, &ofn.lpstrFile[n]); filecount++; mystrcopy(filestrs[filecount], folderstr); mystrcat(filestrs[filecount], &ofn.lpstrFile[n]); fileptrs[filecount]=filestrs[filecount]; //MessageBox(hwnd,fileptrs[filecount],"easyplx",MB_OK); n += mystrlen(&ofn.lpstrFile[n]) + 1 ; } //while (ofn.lpstrFile[n]); //mystrcat(liststr,"EOF"); SetWindowText(GetDlgItem(hwnd, editpic),printstr); returnval=1; } return returnval; } int getm3ufilename(HWND hwnd) { OPENFILENAME ofn; char FileName[MAXLENGTH]; int returnval=0; char printstr[MAXLENGTH]; int n=0; ZeroMemory(&ofn, sizeof(ofn)); FileName[0] = 0; ofn.lStructSize = sizeof(ofn); ofn.hwndOwner = hwnd; ofn.lpstrFilter = "M3U Files (*.m3u)\0*.m3u\0All Files (*.*)\0*.*\0\0"; ofn.lpstrDefExt = "m3u"; ofn.Flags = OFN_EXPLORER | OFN_FILEMUSTEXIST | OFN_HIDEREADONLY; ofn.lpstrFile = FileName; ofn.nMaxFile = MAXLENGTH; if (GetOpenFileName(&ofn)){ listcount=1; mystrcopy(liststrs[listcount],ofn.lpstrFile); listptrs[listcount]=liststrs[listcount]; mystrcopy(printstr,"M3U file: "); mystrcat(printstr,ofn.lpstrFile); SetWindowText(GetDlgItem(hwnd, editm3u),printstr); returnval=1; } return returnval; } int getroot (HWND hwnd, int objectid) { BROWSEINFO bi; char lpBuffer[MAXLENGTH]; char msg[MAXLENGTH]; char dirpath[MAXLENGTH]; LPITEMIDLIST pidlBrowse; // PIDL selected by user char helpstr[MAXLENGTH]; makeutf(helpstr,"Choose the folder to filter from the file paths."); bi.hwndOwner = hwnd; bi.pszDisplayName = lpBuffer; bi.pidlRoot = NULL; bi.lpszTitle = helpstr; bi.ulFlags = 0; bi.lpfn = NULL; bi.lParam = 0; // Browse for a folder and return its PIDL. pidlBrowse = SHBrowseForFolder(&bi); if (pidlBrowse==0) return 1; SHGetPathFromIDList(pidlBrowse, dirpath); notutf(msg,dirpath); if (msg[mystrlen(msg)-1] != '\\'){ mystrcat(msg,"\\"); } SetWindowText(GetDlgItem(hwnd, objectid), msg); return 0; } int maintask (HWND hwnd) { int n=0,m=0,tmp=0,pos=0,startp=0,endp=0,foundeof=0; char ch; char m3ufolder[MAXLENGTH]; char tmpname[MAXLENGTH]; char tmpwork[MAXLENGTH]; //for the files char plxbuf[MAXFILELENGTH]; char listbuf[MAXFILELENGTH]; //add the specified PLX name GetWindowText(GetDlgItem(hwnd, editname),outname,MAXLENGTH); //driveletter GetWindowText(GetDlgItem(hwnd, editletter),driveletter,2); //write the strings from editboxes to the INI file writeini(hwnd); //check for input files if ((filecount == 0) && (listcount == 0)) { haserror=ERROR_NO_FILES; MessageBox(hwnd,"Error: No input files specified! \n","EasyPLX",MB_OK | MBWARNING); return haserror; } //control and insert drive letter if (mystrlen(driveletter)==0){ MessageBox(hwnd,"You have to specify the drive letter for your iriver DAP. Enter the drive letter in the \"Drive Letter\" text box!","EasyPLX",MB_OK | MBWARNING); SetFocus(GetDlgItem(hwnd,editletter)); haserror=ERROR_OUT; return haserror; } //create outpath mystrcopy (outpath, outdir); if (mystrlen(outname)==0){ MessageBox(hwnd,"You have to specify a name for the playlist, in the empty text box!","EasyPLX",MB_OK | MBWARNING); SetFocus(GetDlgItem(hwnd,editname)); haserror=ERROR_OUT; return haserror; } //add drive letter outpath[0]=driveletter[0]; //add the filename to the outpath mystrcat (outpath, outname); //and the file extension mystrcat (outpath, fileext); //MessageBox(hwnd, outpath, "\n\nThe complete output path is: %s", MB_OK); // Preparing makeplx entrycount=filecount; // List processing if (listcount){ //get the folder where the m3u file is located mystrcopy(m3ufolder,listptrs[listcount]); for(m=mystrlen(m3ufolder);m>=0;m--){ if (m3ufolder[m]=='\\') { m3ufolder[m+1]=0; break; } if (m==0) m3ufolder[0]=0; //if no back-slash was found then empty the whole string } //open the list file FILE * infile; char mode[] = "rb"; infile = fopen(listptrs[listcount], mode); if (infile==0) { haserror=ERROR_IN; MessageBox(hwnd,"Error: Couldn't open list file for reading!","EasyPLX",MB_OK | MBERROR); return haserror; } pos=-1; while (ch!=EOF){ pos++; ch=fgetc(infile); listbuf[pos]=ch; } fclose(infile); pos=0; while (1) { //get a start pointer, skip any LF (0x0A) or CR (0x0D) or space bar (0x20) or TAB (0x09) while (listbuf[pos]==0x0A || listbuf[pos]==0x0D || listbuf[pos]==0 || listbuf[pos]==0x09 || listbuf[pos]==' ') pos++; startp = pos; //printf("\nstart found at %d is %d is eof %d",pos,listbuf[pos],listbuf[pos]==EOF); //get an ending pointer at EOF or LF or CR or TAB while (listbuf[pos]!=0x0A && listbuf[pos]!=0x0D && listbuf[pos]!=0 && listbuf[pos]!=0x09 && listbuf[pos]!=EOF) pos++; if (listbuf[pos]==EOF) { foundeof=1; //printf("break"); break; } //rewind through the space bars at the end of a line //(these can not be skipped when searching for CR/LF above cos that would break file names containg spaces) while (listbuf[pos-1]==' ') pos--; endp = pos; //printf("\nend found at %d is %d is eof %d",pos,listbuf[pos],listbuf[pos]==EOF); //end the string with 0x0 listbuf[endp] = 0; //we must skip the lines in the M3U with comments if (listbuf[startp]!='#'){ entrycount++; mystrcopy(filestrs[entrycount],m3ufolder); mystrcat(filestrs[entrycount],&listbuf[startp]); fileptrs[entrycount]=filestrs[entrycount]; } } } //Preparing PLX - the new way for (n=1; n<=entrycount; n++){ mystrcopy(tmpwork,fileptrs[n]); //find the last backslash, if any for(m=mystrlen(tmpwork);m>=0;m--){ if(tmpwork[m]=='\\') break; } mystrcopy(tmpname,tmpwork+m+1); tmpwork[m+1]=0; //find the second last backslash, if any for(m=m-1;m>=0;m--){ if(tmpwork[m]=='\\') break; } mystrcopy(entryfolders[n],tmpwork+m+1); if(tmpwork[m+1]=='.' && tmpwork[m+2]=='.' && tmpwork[m+3]=='\\') mystrcopy(entryfolders[n],"_.._\\"); //if it's a path with ".." if(tmpwork[m+2]==':') mystrcopy(entryfolders[n]+1,"_root_folder_\\"); //if its an absolute path mystrcopy(entrystrs[n],entryfolders[n]); mystrcat(entrystrs[n],tmpname); entryptrs[n]=entrystrs[n]; entrylength[n]=mystrlen(entryptrs[n]); //MessageBox(hwnd,entryptrs[n],"Picture nr %d: %s",MB_OK); } // Making PLX tmp=makeplx(plxbuf, entryptrs, entrylength, entrycount); if (tmp==0) { haserror = ERROR_PLX; MessageBox(hwnd,"Error when creating PLX!","EasyPLX",MB_OK | MBERROR); return haserror; } //Writing PLX to file FILE * outfile; char mode[] = "wb"; outfile = fopen(outpath,mode); if (outfile==0){ haserror=ERROR_OUT; MessageBox(hwnd,"Error: Couldn't open file for writing","EasyPLX",MB_OK | MBERROR); return haserror; } fwrite(plxbuf, tmp, 1, outfile); fclose(outfile); //If the check box isnt checked, then quit. if(!IsDlgButtonChecked(hwnd,checkjfujth)){ MessageBox(hwnd,"PLX file successfully created!","EasyPLX",MB_OK | MBINFO); return haserror; } //The following part is only for JFU and JTH char targetdir[MAXLENGTH]; char filename[MAXLENGTH]; char cmd[MAXLENGTH]; char jfujth[MAXLENGTH]; //Pictures from Browse dialog AND from M3U list for(n=1;n<=entrycount;n++){ mystrcopy(targetdir,jfujthdir); targetdir[0]=driveletter[0]; mystrcat(targetdir,entryfolders[n]); //Check if the target folder exists, and if not, create it if(!SetCurrentDirectory(targetdir)) CreateDirectory(targetdir,0); mystrcopy(filename,entryptrs[n]); //the file extension needs to be removed from the file name for(m=mystrlen(filename);m>=0;m--){ if (filename[m]=='\\') break; if (filename[m]=='.') { filename[m]=0; break; } } mystrcopy(cmd,jfujthdir); cmd[0]=driveletter[0]; mystrcat(cmd,filename); mystrcopy(jfujth,cmd); mystrcat(jfujth,jfuext); if (!CopyFile(fileptrs[n],jfujth,0)){ //MessageBox(hwnd,jfujth,"Error copying",0); haserror=ERROR_COPY; } mystrcopy(jfujth,cmd); mystrcat(jfujth,jthext); if (!CopyFile(fileptrs[n],jfujth,0)){ //MessageBox(hwnd,jfujth,"Error copying",0); haserror=ERROR_COPY; } } //Error handler switch(haserror){ case 0: MessageBox(hwnd,"Picture files and PLX file successfully created!","EasyPLX",MB_OK | MBINFO); break; case ERROR_COPY: MessageBox(hwnd,"The application has finished, but \nerror(s) occured when copying the pictures to the device. ","EasyPLX",MB_OK | MBERROR); break; } return haserror; } int saveset(HWND hwnd) { GetWindowText(GetDlgItem(hwnd, editpath),outdir,MAXLENGTH); GetWindowText(GetDlgItem(hwnd, editext),fileext,MAXLENGTH); GetWindowText(GetDlgItem(hwnd, editjfujthpath),jfujthdir,MAXLENGTH); GetWindowText(GetDlgItem(hwnd, editjfuext),jfuext,MAXLENGTH); GetWindowText(GetDlgItem(hwnd, editjthext),jthext,MAXLENGTH); writeini(hwnd); return 0; } int writeini(HWND hwnd) { char inifn[MAXLENGTH]; FILE* inifile; GetModuleFileName(0,inifn,MAXLENGTH); mystrcat(inifn,".ini"); inifile=fopen(inifn,"w"); if (inifile==0) return 1; fputs(outdir,inifile); fputs("\n",inifile); fputs(fileext,inifile); fputs("\n",inifile); fputs(jfujthdir,inifile); fputs("\n",inifile); fputs(jfuext,inifile); fputs("\n",inifile); fputs(jthext,inifile); fputs("\n",inifile); fputs(INI_SUFFIX,inifile); fclose(inifile); return 0; } int readini(HWND hwnd) { char inifn[MAXLENGTH]; FILE* inifile; GetModuleFileName(0,inifn,MAXLENGTH); mystrcat(inifn,".ini"); inifile=fopen(inifn,"r"); if (inifile==0) { mystrcopy(outdir,OUT_DIR_DEFAULT); mystrcopy(fileext,FILE_EXT); mystrcopy(jfujthdir,JFU_JTH_DIR_DEFAULT); mystrcopy(jfuext,JFU_EXT); mystrcopy(jthext,JTH_EXT); return 1; } void iniset(int control,char* tmp){ fgets(tmp,MAXLENGTH,inifile); if(tmp[mystrlen(tmp)-1]==0x0A || tmp[mystrlen(tmp)-1]==0x0D) tmp[mystrlen(tmp)-1]=0; if(tmp[mystrlen(tmp)-2]==0x0A || tmp[mystrlen(tmp)-2]==0x0D) tmp[mystrlen(tmp)-2]=0; //SetWindowText(GetDlgItem(hwnd,control),tmp); } iniset(editpath,outdir); iniset(editext,fileext); iniset(editjfujthpath,jfujthdir); iniset(editjfuext,jfuext); iniset(editjthext,jthext); fclose(inifile); return 0; } int deleteini(HWND hwnd) { char inifn[MAXLENGTH]; FILE* inifile; GetModuleFileName(0,inifn,MAXLENGTH); mystrcat(inifn,".ini"); DeleteFile(inifn); return 0; } int showiniset(HWND hwnd) { SetWindowText(GetDlgItem(hwnd,editpath),outdir); SetWindowText(GetDlgItem(hwnd,editext),fileext); SetWindowText(GetDlgItem(hwnd,editjfujthpath),jfujthdir); SetWindowText(GetDlgItem(hwnd,editjfuext),jfuext); SetWindowText(GetDlgItem(hwnd,editjthext),jthext); return 0; } int restart(HWND hwnd) { char cmd[MAXLENGTH]; char exepath[MAXLENGTH]; //This function is used to get the file name of the calling program //the zero means "calling program" //thanks to anthrax11 GetModuleFileName(0, exepath, MAXLENGTH); mystrcopy(cmd, "START "); mystrcat(cmd,exepath); system(cmd); return 0; } int quit(HWND hwnd) { DestroyWindow(hwnd); return 0; } int about(HWND hwnd) { char printstr[MAXFILELENGTH]; mystrcopy(printstr,HELP_HEADER); mystrcat(printstr,VERSION); mystrcat(printstr,HELP_MAIN); MessageBox(hwnd, printstr, "EasyPLX", MB_OK | MBINFO); return 0; } int help(HWND hwnd) { MessageBox(hwnd, HELP_HOWTO, "EasyPLX", MB_OK | MBINFO); return 0; }