/** * Copy a track to the device */ MediaItem *MtpMediaDevice::copyTrackToDevice( const MetaBundle &bundle ) { DEBUG_BLOCK QString genericError = i18n( "Could not send track" ); LIBMTP_track_t *trackmeta = LIBMTP_new_track_t(); trackmeta->item_id = 0; debug() << "filetype : " << bundle.fileType() << endl; if( bundle.fileType() == MetaBundle::mp3 ) { trackmeta->filetype = LIBMTP_FILETYPE_MP3; } else if( bundle.fileType() == MetaBundle::ogg ) { trackmeta->filetype = LIBMTP_FILETYPE_OGG; } else if( bundle.fileType() == MetaBundle::wma ) { trackmeta->filetype = LIBMTP_FILETYPE_WMA; } else if( bundle.fileType() == MetaBundle::mp4 ) { trackmeta->filetype = LIBMTP_FILETYPE_MP4; } else { // Couldn't recognise an Pana filetype. // fallback to checking the extension (e.g. .wma, .ogg, etc) debug() << "No filetype found by Pana filetype" << endl; const QString extension = bundle.url().path().section( ".", -1 ).lower(); int libmtp_type = m_supportedFiles.findIndex( extension ); if( libmtp_type >= 0 ) { int keyIndex = mtpFileTypes.values().findIndex( extension ); libmtp_type = mtpFileTypes.keys()[keyIndex]; trackmeta->filetype = (LIBMTP_filetype_t) libmtp_type; debug() << "set filetype to " << libmtp_type << " based on extension of ." << extension << endl; } else { debug() << "We don't support the extension ." << extension << endl; Pana::StatusBar::instance()->shortLongMessage( genericError, i18n( "Cannot determine a valid file type" ), KDE::StatusBar::Error ); return 0; } } if( bundle.title().isEmpty() ) { trackmeta->title = qstrdup( i18n( "Unknown title" ).utf8() ); } else { trackmeta->title = qstrdup( bundle.title().utf8() ); } if( bundle.album().isEmpty() ) { trackmeta->album = qstrdup( i18n( "Unknown album" ).utf8() ); } else { trackmeta->album = qstrdup( bundle.album().string().utf8() ); } if( bundle.artist().isEmpty() ) { trackmeta->artist = qstrdup( i18n( "Unknown artist" ).utf8() ); } else { trackmeta->artist = qstrdup( bundle.artist().string().utf8() ); } if( bundle.genre().isEmpty() ) { trackmeta->genre = qstrdup( i18n( "Unknown genre" ).utf8() ); } else { trackmeta->genre = qstrdup( bundle.genre().string().utf8() ); } if( bundle.year() > 0 ) { QString date; QTextOStream( &date ) << bundle.year() << "0101T0000.0"; trackmeta->date = qstrdup( date.utf8() ); } else { trackmeta->date = qstrdup( "00010101T0000.0" ); } if( bundle.track() > 0 ) { trackmeta->tracknumber = bundle.track(); } if( bundle.length() > 0 ) { // Multiply by 1000 since this is in milliseconds trackmeta->duration = bundle.length() * 1000; } if( !bundle.filename().isEmpty() ) { trackmeta->filename = qstrdup( bundle.filename().utf8() ); } trackmeta->filesize = bundle.filesize(); // try and create the requested folder structure uint32_t parent_id = 0; if( !m_folderStructure.isEmpty() ) { parent_id = checkFolderStructure( bundle ); if( parent_id == 0 ) { debug() << "Couldn't create new parent (" << m_folderStructure << ")" << endl; Pana::StatusBar::instance()->shortLongMessage( genericError, i18n( "Cannot create parent folder. Check your structure." ), KDE::StatusBar::Error ); return 0; } } else { parent_id = getDefaultParentId(); } debug() << "Parent id : " << parent_id << endl; m_critical_mutex.lock(); debug() << "Sending track... " << bundle.url().path().utf8() << endl; int ret = LIBMTP_Send_Track_From_File( m_device, bundle.url().path().utf8(), trackmeta, progressCallback, this ); m_critical_mutex.unlock(); if( ret < 0 ) { debug() << "Could not write file " << ret << endl; Pana::StatusBar::instance()->shortLongMessage( genericError, i18n( "File write failed" ), KDE::StatusBar::Error ); return 0; } MetaBundle temp( bundle ); MtpTrack *taggedTrack = new MtpTrack( trackmeta ); taggedTrack->setBundle( temp ); taggedTrack->setFolderId( parent_id ); LIBMTP_destroy_track_t( trackmeta ); kapp->processEvents( 100 ); // add track to view and to new tracks list MediaItem *newItem = addTrackToView( taggedTrack ); m_newTracks->append( newItem ); return newItem; }
int copyFile(char *source_file, char *new_file){ int sFile_fd, nDir_fd, nFile_fd; char buffer[BUFFER_SIZE]; int num_read; long totalSize = 0; // Hedef konum bilgisi parse edilerek dosya adi bilgisi alinir. char *filename = basename(new_file); // Hedef konum bilgisi parse edilerek path bilgisi alinir. char *directory = dirname(new_file); // Kaynak dosya acilir. Dosyanin acilmasi sirasinda bir hata olusursa // ilgili hata bilgisi ekrana yazdirilir. sFile_fd = open(source_file, O_RDONLY, 0); if (sFile_fd == -1) { perror("Error opening source file"); exit(1); } // Kopyalama isleminin, verilen klasor yapisina uygun hale getirilmesi // icin bu fonksiyon cagrilarak eksik klasorler olusturulur. checkFolderStructure(directory, &nDir_fd); // Arguman olarak verilmis olan newPath degiskenin icerigi basename/dirname // fonksiyonlari tarafindan degistirildigi icin burada kucuk bir concat // islemi yapilir. sprintf(new_file, "%s/%s", newPath, filename); // Yeni dosya, ilgili dosya adiyla, yazilmak uzere acilir/olusturulur. // Dosyanin acilmasi sirasinda bir hata olusursa // ilgili hata bilgisi ekrana yazdirilir. nFile_fd = open(new_file, O_WRONLY | O_EXCL | O_CREAT, 0644); if (nFile_fd == -1) { perror("Error opening new file"); if (errno == EISDIR) printf("Hedef dosyayi [/dosya] seklinde, basinda / isaretiyle girmeniz geriyor olabilir.\n"); exit(1); } // Kaynak dosyadan, buffer boyutu kadar byte okunur. Eger okuma // sonucunda 0 dan farkli sayida byte okunmus ise islem basarilidir, // donguye tekrar girilir. Okunan bytelar, yeni dosyaya yazilir ve // yazilan toplam byte sayisinin tutuldugu totalSize degiskeninin // icerigi guncellenir. while ((num_read = read(sFile_fd, &buffer, BUFFER_SIZE)) > 0) { write(nFile_fd, &buffer, num_read); totalSize = totalSize + num_read; } // Kopyalanan toplam byte bilgisi ekrana yazdirilir. printf("\nToplam %ld byte kopyalandi.\n\n", totalSize); // Tum file handler'lar kapatilir. // Source file descriptor, New directory file descriptor ve New file file descriptor close(sFile_fd); close(nDir_fd); close(nFile_fd); }