[Digikam-devel] [digikam] utilities/importui/views: fix CameraNameHelper: the unittests were failing due to changes in GPhoto2. Cleanup code, simplify code
Andi Clemens
andi.clemens at googlemail.com
Wed Oct 3 16:28:51 BST 2012
Git commit 42548516dc62e83f9a7a329284c3ebaf5f90ba40 by Andi Clemens.
Committed on 03/10/2012 at 17:22.
Pushed by aclemens into branch 'master'.
fix CameraNameHelper: the unittests were failing due to changes in GPhoto2. Cleanup code, simplify code
We have a lot more unittests that are currently failing, we should fix the issues!!
CCMAIL:digikam-devel at kde.org
M +52 -83 utilities/importui/views/cameranamehelper.cpp
M +0 -2 utilities/importui/views/cameranamehelper.h
http://commits.kde.org/digikam/42548516dc62e83f9a7a329284c3ebaf5f90ba40
diff --git a/utilities/importui/views/cameranamehelper.cpp b/utilities/importui/views/cameranamehelper.cpp
index 03a0e8f..6f8b12a 100644
--- a/utilities/importui/views/cameranamehelper.cpp
+++ b/utilities/importui/views/cameranamehelper.cpp
@@ -26,11 +26,21 @@
// Qt includes
#include <QAction>
+#include <QRegExp>
// KDE includes
#include <klocale.h>
+namespace
+{
+static const QString STR_AUTO_DETECTED("auto-detected");
+
+static QRegExp REGEXP_CAMERA_NAME("^(.*)\\s*\\((.*)\\)\\s*$", Qt::CaseInsensitive);
+static QRegExp REGEXP_MODES("^(ptp|normal|mtp)(\\s+mode)?$", Qt::CaseInsensitive);
+static QRegExp REGEXP_AUTODETECTED(QString("(%1|, %1)").arg(STR_AUTO_DETECTED));
+}
+
namespace Digikam
{
@@ -54,12 +64,12 @@ QString CameraNameHelper::createCameraName(const QString& vendor, const QString&
tmp.append(" (");
tmp.append(_mode);
tmp.append(autoDetected
- ? QString(", %1)").arg(autoDetectedString())
+ ? QString(", %1)").arg(STR_AUTO_DETECTED)
: QString(')'));
}
else if (autoDetected)
{
- tmp.append(QString(" (%1)").arg(autoDetectedString()));
+ tmp.append(QString(" (%1)").arg(STR_AUTO_DETECTED));
}
return tmp.simplified();
@@ -78,90 +88,67 @@ QString CameraNameHelper::formattedFullCameraName(const QString& name, bool auto
QString CameraNameHelper::parseAndFormatCameraName(const QString& cameraName,
bool parseMode, bool autoDetected)
{
- QString tmp;
+ if (!parseMode && !autoDetected)
+ {
+ return cameraName.simplified();
+ }
QString vendorAndProduct = extractCameraNameToken(cameraName, VendorAndProduct);
- QString mode = parseMode
- ? extractCameraNameToken(cameraName, Mode)
- : QString();
if (vendorAndProduct.isEmpty())
{
return QString();
}
- // we split vendorAndProduct once, it doesn't really matter if the variables are correctly filled,
- // it is only important that both are not empty.
- QStringList words = vendorAndProduct.split(' ');
- QString vendor;
- QString product;
-
- if (words.count() > 1)
- {
- vendor = words.takeFirst();
- product = words.join(" ");
- }
+ QString mode = parseMode
+ ? extractCameraNameToken(cameraName, Mode)
+ : QString();
- tmp = createCameraName(vendor, product, mode, autoDetected);
+ QString tmp = createCameraName(vendorAndProduct, QString(), mode, autoDetected);
return tmp.isEmpty()
- ? cameraName
+ ? cameraName.simplified()
: tmp;
}
-QString CameraNameHelper::autoDetectedString()
-{
- return i18n("auto-detected");
-}
-
QString CameraNameHelper::extractCameraNameToken(const QString& cameraName, Token tokenID)
{
- QStringList capturedTexts;
- QString tmp;
-
- capturedTexts = cameraName.split(" (");
+ REGEXP_CAMERA_NAME.setMinimal(true);
+ REGEXP_MODES.setMinimal(true);
+ REGEXP_AUTODETECTED.setMinimal(true);
- // TODO: Right now we just assume that a camera name has no parentheses in it
- // There is a testcase (CameraNameHelperTest::testCameraNameFromGPCamera) that
- // checks all camera names delivered by gphoto2. At the moment all seems to be fine.
- if (!capturedTexts.isEmpty())
+ if (REGEXP_CAMERA_NAME.exactMatch(cameraName.simplified()))
{
+ QString vendorProduct = REGEXP_CAMERA_NAME.cap(1).simplified();
+ QString tmpMode = REGEXP_CAMERA_NAME.cap(2).simplified();
+ QString clearedTmpMode = tmpMode;
QString mode;
- QString vendorAndProduct;
+ clearedTmpMode.remove(REGEXP_AUTODETECTED);
- if (capturedTexts.count() == 1) // camera name only
+ if (!tmpMode.isEmpty() && clearedTmpMode.isEmpty())
{
- vendorAndProduct = capturedTexts.takeFirst();
+ mode = tmpMode;
}
else
{
- mode = capturedTexts.takeLast().simplified();
- vendorAndProduct = capturedTexts.join((" ")).simplified();
+ mode = REGEXP_MODES.exactMatch(clearedTmpMode)
+ ? clearedTmpMode
+ : "";
}
if (tokenID == VendorAndProduct)
{
- tmp = vendorAndProduct;
+ return mode.isEmpty()
+ ? cameraName.simplified()
+ : vendorProduct;
}
- else if (tokenID == Mode)
+ else
{
- tmp = mode;
+ return mode;
}
-
- }
-
- // clean up the string
- QStringList words = tmp.split((' '));
- tmp.clear();
-
- foreach(const QString& word, words)
- {
- tmp.append(word.simplified());
- tmp.append(' ');
}
-
- return tmp.isEmpty()
+ return (tokenID == VendorAndProduct)
? cameraName.simplified()
- : tmp.simplified();
+ : "";
}
bool CameraNameHelper::sameDevices(const QString& deviceA, const QString& deviceB)
@@ -185,41 +172,23 @@ bool CameraNameHelper::sameDevices(const QString& deviceA, const QString& device
// try to clean up the string, if not possible, return false
if (cameraNameA != cameraNameB)
{
- QString tmpA = prepareStringForDeviceComparison(cameraNameA, VendorAndProduct);
- QString tmpB = prepareStringForDeviceComparison(cameraNameB, VendorAndProduct);
-
- if (tmpA != tmpB)
- {
- return false;
- }
- }
-
- // now check if the mode is the same
- QString modeA = extractCameraNameToken(deviceA, Mode);
- QString modeB = extractCameraNameToken(deviceB, Mode);
-
- // remove the 'mode' token for comparison
- QString strippedModeA = prepareStringForDeviceComparison(modeA, Mode);
- QString strippedModeB = prepareStringForDeviceComparison(modeB, Mode);
-
- if (strippedModeA == strippedModeB)
- {
- return true;
+ return false;
}
- return false;
-}
+ // is the extracted mode known and equal?
+ QString modeA = extractCameraNameToken(deviceA, Mode);
+ QString modeB = extractCameraNameToken(deviceB, Mode);
+ bool isModeAValid = REGEXP_MODES.exactMatch(modeA);
+ modeA = isModeAValid ? REGEXP_MODES.cap(1).simplified().toLower() : "";
+ bool isModeBValid = REGEXP_MODES.exactMatch(modeB);
+ modeB = isModeBValid ? REGEXP_MODES.cap(1).simplified().toLower() : "";
-QString CameraNameHelper::prepareStringForDeviceComparison(const QString& string, Token tokenID)
-{
- QString tmp = string.toLower().remove(QChar('(')).remove(QChar(')')).remove(autoDetectedString()).simplified();
-
- if (tokenID == Mode)
+ if ((isModeAValid != isModeBValid) || (modeA != modeB))
{
- tmp = tmp.remove("mode").remove(QChar(','));
+ return false;
}
- return tmp.simplified();
+ return true;
}
} // namespace Digikam
diff --git a/utilities/importui/views/cameranamehelper.h b/utilities/importui/views/cameranamehelper.h
index 04b8c01..2d081dc 100644
--- a/utilities/importui/views/cameranamehelper.h
+++ b/utilities/importui/views/cameranamehelper.h
@@ -60,8 +60,6 @@ private:
static QString extractCameraNameToken(const QString& cameraName, Token tokenID);
static QString parseAndFormatCameraName(const QString& cameraName, bool parseMode, bool autoDetected);
- static QString autoDetectedString();
- static QString prepareStringForDeviceComparison(const QString& string, Token tokenID);
};
} // namespace Digikam
More information about the Digikam-devel
mailing list