QString MetaEngine::Private::detectEncodingAndDecode(const std::string& value) const { // For charset autodetection, we could use sophisticated code // (Mozilla chardet, KHTML's autodetection, QTextCodec::codecForContent), // but that is probably too much. // We check for UTF8, Local encoding and ASCII. // Look like KEncodingDetector class can provide a full implementation for encoding detection. if (value.empty()) { return QString(); } if (isUtf8(value.c_str())) { return QString::fromUtf8(value.c_str()); } // Utf8 has a pretty unique byte pattern. // Thats not true for ASCII, it is not possible // to reliably autodetect different ISO-8859 charsets. // So we can use either local encoding, or latin1. return QString::fromLocal8Bit(value.c_str()); }
void M3UParser::save(const QList<MediaInfo> & files) { QTime timeElapsed; timeElapsed.start(); _stop = false; qDebug() << __FUNCTION__ << "Playlist:" << _filename; QString path(QFileInfo(_filename).path()); QFile file(_filename); if (file.open(QIODevice::WriteOnly)) { QTextStream stream(&file); if (isUtf8()) { stream.setCodec("UTF-8"); } else { stream.setCodec(QTextCodec::codecForLocale()); } QString filename; if (!files.isEmpty()) { stream << "#EXTM3U\n"; } foreach (MediaInfo mediaInfo, files) { if (_stop) { break; } stream << "#EXTINF:"; stream << mediaInfo.lengthSeconds(); stream << ','; QString artist(mediaInfo.metadataValue(MediaInfo::Artist)); if (!artist.isEmpty()) { stream << artist; } QString title(mediaInfo.metadataValue(MediaInfo::Title)); if (!title.isEmpty()) { if (!artist.isEmpty()) { stream << " - "; } stream << title; } stream << M3U_EOL; if (mediaInfo.isUrl()) { stream << mediaInfo.fileName(); } else { //Try to save the filename as relative instead of absolute QString filename(TkFile::relativeFilePath(path, mediaInfo.fileName())); stream << Util::pathToNativeSeparators(filename); } stream << M3U_EOL; } }
QList<QString> ParserM3u::parse(QString sFilename) { QFile file(sFilename); QString basepath = sFilename.section('/', 0, -2); clearLocations(); //qDebug() << "ParserM3u: Starting to parse."; if (file.open(QIODevice::ReadOnly) && !isBinary(sFilename)) { /* Unfortunately, QT 4.7 does not handle <CR> (=\r or asci value 13) line breaks. * This is important on OS X where iTunes, e.g., exports M3U playlists using <CR> * rather that <LF> * * Using QFile::readAll() we obtain the complete content of the playlist as a ByteArray. * We replace any '\r' with '\n' if applicaple * This ensures that playlists from iTunes on OS X can be parsed */ QByteArray ba = file.readAll(); //detect encoding bool isCRLF_encoded = ba.contains("\r\n"); bool isCR_encoded = ba.contains("\r"); if(isCR_encoded && !isCRLF_encoded) ba.replace('\r','\n'); QTextStream textstream(ba.constData()); if (isUtf8(ba.constData())) { textstream.setCodec("UTF-8"); } else { textstream.setCodec("windows-1252"); } while(!textstream.atEnd()) { QString sLine = getFilepath(&textstream, basepath); if(sLine.isEmpty()) break; //qDebug() << "ParserM3u: parsed: " << (sLine); m_sLocations.append(sLine); } file.close(); if(m_sLocations.count() != 0) return m_sLocations; else return QList<QString>(); // NULL pointer returned when no locations were found } file.close(); return QList<QString>(); //if we get here something went wrong }
// Construct a dialog box with an optional check box HistInputDialog::HistInputDialog(FXWindow* w,FXString inp,FXString message,FXString title,FXString label,FXIcon *ic, FXuint browse, FXbool option, FXString optiontext): DialogBox(w,title,DECOR_TITLE|DECOR_BORDER|DECOR_STRETCHABLE) { // Browse type flag browsetype=browse; // Buttons buttons=new FXHorizontalFrame(this,LAYOUT_SIDE_BOTTOM|LAYOUT_FILL_X,0,0,0,0,10,10,5,5); // Accept new FXButton(buttons,_("&Accept"),NULL,this,ID_ACCEPT,FRAME_RAISED|FRAME_THICK|LAYOUT_RIGHT,0,0,0,0,20,20); // Cancel new FXButton(buttons,_("&Cancel"),NULL,this,ID_CANCEL,FRAME_RAISED|FRAME_THICK|LAYOUT_RIGHT,0,0,0,0,20,20); // Separator new FXHorizontalSeparator(this,LAYOUT_SIDE_BOTTOM|LAYOUT_FILL_X|SEPARATOR_GROOVE); // Optional check box checkbutton=new FXHorizontalFrame(this,JUSTIFY_RIGHT|LAYOUT_SIDE_BOTTOM|LAYOUT_FILL_X,0,0,0,0,10,10,0,0); if (option) new FXCheckButton(checkbutton,optiontext,this,ID_TOGGLE_OPTION); // Vertical frame FXVerticalFrame *contents=new FXVerticalFrame(this,LAYOUT_SIDE_TOP|FRAME_NONE|LAYOUT_FILL_X|LAYOUT_FILL_Y); // Icon and first line FXMatrix *matrix1 = new FXMatrix(contents,2,MATRIX_BY_COLUMNS|LAYOUT_SIDE_TOP|LAYOUT_FILL_X|LAYOUT_FILL_Y); new FXLabel(matrix1,"",ic,LAYOUT_CENTER_Y|LAYOUT_FILL_COLUMN|LAYOUT_FILL_ROW); new FXLabel(matrix1,message,NULL,JUSTIFY_LEFT|LAYOUT_CENTER_Y|LAYOUT_FILL_COLUMN|LAYOUT_FILL_ROW); new FXLabel(matrix1,label,NULL,LAYOUT_RIGHT|LAYOUT_CENTER_Y|LAYOUT_FILL_COLUMN|LAYOUT_FILL_ROW); // Label and input field (combo box) FXMatrix *matrix2 = new FXMatrix(contents,3,MATRIX_BY_COLUMNS|LAYOUT_SIDE_TOP|LAYOUT_FILL_X|LAYOUT_FILL_Y); new FXLabel(matrix2,label,NULL,LAYOUT_LEFT|LAYOUT_CENTER_Y|LAYOUT_FILL_ROW); input = new ComboBox(matrix2,40,NULL,0,COMBOBOX_INSERT_LAST|LAYOUT_CENTER_Y|LAYOUT_CENTER_X|LAYOUT_FILL_COLUMN|LAYOUT_FILL_ROW|LAYOUT_FILL_X); input->setNumVisible(5); input->setText(inp); new FXButton(matrix2,_("\tSelect destination..."),filedialogicon,this,ID_BROWSE_PATH,FRAME_RAISED|FRAME_THICK|LAYOUT_RIGHT|LAYOUT_CENTER_Y,0,0,0,0,20,20); if (!isUtf8(message.text(),message.length())) new FXLabel(contents,_("=> Warning: file name is not UTF-8 encoded!"),NULL,LAYOUT_LEFT|LAYOUT_CENTER_Y|LAYOUT_FILL_ROW); // Initial directory for browsing FXString homelocation=getenv("HOME"); if(homelocation=="") homelocation=ROOTDIR; initialdir=homelocation; }
char * BtStringMgr::upperUTF8(char * text, unsigned int maxlen) const { size_t max = (maxlen > 0u) ? maxlen : strlen(text); if (LIKELY(max > 1u)) { max--; if (isUtf8(text)) { strncpy(text, QString::fromUtf8(text).toUpper().toUtf8().constData(), max); } else { strncpy(text, QString::fromLatin1(text).toUpper().toLatin1().constData(), max); } text[max] = '\0'; } else if (max == 1u) { text[0u] = '\0'; } else { Q_ASSERT(max == 0u); } return text; }
char* MSStringMgr::upperUTF8(char* text, const unsigned int maxlen) { if (isUtf8(text)) { const int max = (maxlen>0) ? maxlen : strlen(text); CFStringRef cfString; CFMutableStringRef mstr; cfString = CFStringCreateWithBytes (NULL, (UInt8*)text, max, kCFStringEncodingUTF8, false); if(!cfString) return text; mstr = CFStringCreateMutableCopy(NULL, 0, cfString); CFStringUppercase(mstr, NULL); CFStringGetBytes ( mstr, CFRangeMake(0, CFStringGetLength(mstr)), kCFStringEncodingUTF8, 0, false, (UInt8*)text, max, NULL ); CFRelease(cfString); CFRelease(mstr); return text; } else { char* ret = text; while (*text) { *text = toupper(*text); text++; } return ret; } return text; }
void M3UParser::load() { QTime timeElapsed; timeElapsed.start(); _stop = false; QList<MediaInfo> files; qDebug() << __FUNCTION__ << "Playlist:" << _filename; //See http://regexlib.com/DisplayPatterns.aspx //#EXTM3U static QRegExp rx_extm3u("^#EXTM3U$|^#M3U$"); //#EXTINF:123,Sample title static QRegExp rx_extinf("^#EXTINF:([-+]?\\d+),(.*)$"); //#EXTINF:Sample title static QRegExp rx_extinf_title("^#EXTINF:(.*)$"); //#Just a comment static QRegExp rx_comment("^#.*$"); QFile file(_filename); if (file.open(QIODevice::ReadOnly)) { QString path(QFileInfo(_filename).path()); QTextStream stream(&file); if (isUtf8()) { stream.setCodec("UTF-8"); } else { stream.setCodec(QTextCodec::codecForLocale()); } MediaInfo mediaInfo; while (!stream.atEnd() && !_stop) { //Line of text excluding '\n' QString line(stream.readLine().trimmed()); if (line.isEmpty()) { //Do nothing } else if (rx_extm3u.indexIn(line) != -1) { //#EXTM3U line, ignored } else if (rx_extinf.indexIn(line) != -1) { //#EXTINF line QString length(rx_extinf.cap(1)); if (!length.isEmpty()) { mediaInfo.setLength(length.toInt()); } QString title(rx_extinf.cap(2)); if (!title.isEmpty()) { mediaInfo.insertMetadata(MediaInfo::Title, title); } } else if (rx_extinf_title.indexIn(line) != -1) { QString title(rx_extinf_title.cap(1)); if (!title.isEmpty()) { mediaInfo.insertMetadata(MediaInfo::Title, title); } } else if (rx_comment.indexIn(line) != -1) { //# line, comment, ignored } else { bool isUrl = MediaInfo::isUrl(line); mediaInfo.setUrl(isUrl); if (isUrl) { mediaInfo.setFileName(line); } else { mediaInfo.setFileName(Util::canonicalFilePath(path, line)); } //Add file to the list of files files << mediaInfo; //Clear the MediaInfo for the next 2 lines from the m3u playlist mediaInfo.clear(); if (files.size() > FILES_FOUND_LIMIT) { //Emits the signal every FILES_FOUND_LIMIT files found emit filesFound(files); files.clear(); } } } } file.close(); if (!files.isEmpty()) { //Emits the signal for the remaining files found (< FILES_FOUND_LIMIT) emit filesFound(files); } //Emits the last signal emit finished(timeElapsed.elapsed()); }