From a6ec5ea9de36a24f44998887d34f631bebd59864 Mon Sep 17 00:00:00 2001 From: "Martin T. H. Sandsmark" Date: Sun, 24 Jul 2016 20:27:04 +0200 Subject: [PATCH] Fix parsing of lilypond URLs --- core/sourcereference.cpp | 55 ++++++++++++++++++++++------------------ 1 file changed, 31 insertions(+), 24 deletions(-) diff --git a/core/sourcereference.cpp b/core/sourcereference.cpp index a9d72d605..bde8bc1bd 100644 --- a/core/sourcereference.cpp +++ b/core/sourcereference.cpp @@ -59,33 +59,40 @@ int SourceReference::column() const bool Okular::extractLilyPondSourceReference( const QUrl &url, QString *file, int *row, int *col ) { + // Example URL is: textedit:///home/foo/bar.ly:42:42:42 + // The three numbers are apparently: line:beginning of column:end of column + if ( url.scheme() != QStringLiteral("textedit") ) return false; -#pragma message("KF5 fix LilyPond references") - return false; - -// *row = 0; -// *col = 0; -// int lilyChar = 0; -// typedef int *IntPtr; -// const IntPtr int_data[] = { row, &lilyChar, col }; -// int int_index = sizeof( int_data ) / sizeof( int* ) - 1; -// int index_last = -1; -// int index = url.lastIndexOf( QLatin1Char( ':' ), index_last ); -// while ( index != -1 && int_index >= 0 ) -// { -// // read the current "chunk" -// const QStringRef ref = url.midRef( index + 1, index_last - index - 1 ); -// *int_data[ int_index ] = QString::fromRawData( ref.data(), ref.count() ).toInt(); -// // find the previous "chunk" -// index_last = index; -// index = url.lastIndexOf( QLatin1Char( ':' ), index_last - 1 ); -// --int_index; -// } -// // NOTE: 11 is the length of "textedit://" -// *file = QUrl::fromPercentEncoding( url.mid( 11, index_last != -1 ? index_last - 11 : -1 ).toUtf8() ); -// return true; + // There can be more, in case the filename contains : + if (url.fileName().count(':') < 3) { + return false; + } + + QStringList parts(url.path().split(':')); + + bool ok; + // Take out the things we need + int columnEnd = parts.takeLast().toInt(&ok); // apparently we don't use this + Q_UNUSED(columnEnd); + if (!ok) { + return false; + } + + *col = parts.takeLast().toInt(&ok); + if (!ok) { + return false; + } + + *row = parts.takeLast().toInt(&ok); + if (!ok) { + return false; + } + + // In case the path itself contains :, we need to reconstruct it after removing all the numbers + *file = parts.join(':'); + return (!file->isEmpty()); } QString Okular::sourceReferenceToolTip( const QString &source, int row, int col )