hi, guys!

In previous thread I came to the conclusion that the best way to
proceed with that nasty no-default-style-gets picked bug is to start
using style names rather then style paths, i.e. convert names to paths
in only several places where it is actually needed. These seems to
simplify code a bit and fix bug :)

So here's the patch that makes kopete use style names instead of style Paths.
I hope I not forgot anything.
It works for me here in all cases (upon first run of ChatMessagePart
and during selection of styles in config dialog).
One QHash (key=styleName, value=Path) got removed, because now only
names are stored - in a simple QStringLists

Well, check it out and tell me what do you think.
There are still some thing to do - fix apidocs (which still refer to
stylePaths) and remove usage of  "slash-hack" in two functions in
KopeteStyleManager. I'll do that later, if you agree with current
proposal :).

I tried hard not to mess up your tab based indentation (sorry, but
it's weird! while kdelibs and most other modules use spaces, kopete
uses tabs - very unpleasant for newcomer :-P)

/me is sitting and waiting for a feedback

Dmitry.
Index: kopete/config/chatwindow/chatwindowconfig.h
===================================================================
--- kopete/config/chatwindow/chatwindowconfig.h	(revision 695906)
+++ kopete/config/chatwindow/chatwindowconfig.h	(working copy)
@@ -61,9 +61,7 @@
 	//----- Style TAB ----------------------
 	Ui::ChatWindowConfig_Style m_styleUi;
 	ChatMessagePart *m_preview;
-	
-	// value is the style path
-	QMap<Q3ListBoxItem*,QString> m_styleItemMap;
+
 	ChatWindowStyle::StyleVariants m_currentVariantMap;
 	ChatWindowStyle *m_currentStyle;
 	bool m_loading;
Index: kopete/config/chatwindow/chatwindowconfig.cpp
===================================================================
--- kopete/config/chatwindow/chatwindowconfig.cpp	(revision 695906)
+++ kopete/config/chatwindow/chatwindowconfig.cpp	(working copy)
@@ -213,11 +213,11 @@
 
 	KopeteChatWindowSettings *settings = KopeteChatWindowSettings::self();
 
-	// Get the stylePath
+	// Get the styleName
 	if(m_currentStyle)
 	{
-		kDebug(14000) << k_funcinfo << m_currentStyle->getStylePath();
-		settings->setStylePath( m_currentStyle->getStylePath() );
+		kDebug(14000) << k_funcinfo << m_currentStyle->getStyleName();
+		settings->setStyleName( m_currentStyle->getStyleName() );
 	}
 	// Get and save the styleVariant
 	if( !m_currentVariantMap.empty() )
@@ -250,24 +250,20 @@
 void ChatWindowConfig::slotLoadChatStyles()
 {
 	m_styleUi.styleList->clear();
-	m_styleItemMap.clear();
 
-	ChatWindowStyleManager::StyleList availableStyles;
+	QStringList availableStyles;
 	availableStyles = ChatWindowStyleManager::self()->getAvailableStyles();
 	if( availableStyles.empty() )
 		kDebug(14000) << k_funcinfo << "Warning, available styles is empty !";
 
-	ChatWindowStyleManager::StyleList::ConstIterator it, itEnd = availableStyles.constEnd();
-	for(it = availableStyles.constBegin(); it != itEnd; ++it)
+	foreach( const QString& styleName, availableStyles )
 	{
 		// Insert style name into the listbox
-		m_styleUi.styleList->insertItem( it.key(), 0 );
-		// Insert the style class into the internal map for futher access.
-		m_styleItemMap.insert( m_styleUi.styleList->firstItem(), it.value() );
+		m_styleUi.styleList->insertItem( styleName, 0 );
 
-		if( it.value() == KopeteChatWindowSettings::self()->stylePath() )
+		if( styleName == KopeteChatWindowSettings::self()->styleName() )
 		{
-			kDebug(14000) << k_funcinfo << "Restoring saved style: " << it.key();
+			kDebug(14000) << k_funcinfo << "Restoring saved style: " << styleName;
 
 			m_styleUi.styleList->setSelected( m_styleUi.styleList->firstItem(), true );
 		}
@@ -280,13 +276,13 @@
 void ChatWindowConfig::slotChatStyleSelected()
 {
 	// Retrieve variant list.
-	QString stylePath = m_styleItemMap[m_styleUi.styleList->selectedItem()];
-	m_currentStyle = ChatWindowStyleManager::self()->getStyleFromPool( stylePath );
+	QString styleName = m_styleUi.styleList->selectedItem()->text();
+	m_currentStyle = ChatWindowStyleManager::self()->getStyleFromPool( styleName );
 
 	if(m_currentStyle)
 	{
 		m_currentVariantMap = m_currentStyle->getVariants();
-		kDebug(14000) << k_funcinfo << "Loading style: " << m_currentStyle->getStylePath();
+		kDebug(14000) << k_funcinfo << "Loading style: " << m_currentStyle->getStyleName();
 
 		// Update the variant list based on current style.
 		m_styleUi.variantList->clear();
Index: kopete/chatwindow/kopetechatwindowstylemanager.h
===================================================================
--- kopete/chatwindow/kopetechatwindowstylemanager.h	(revision 695906)
+++ kopete/chatwindow/kopetechatwindowstylemanager.h	(working copy)
@@ -48,13 +48,6 @@
 	Q_OBJECT
 public:
 	/**
-	 * StyleList typedef (a QMap)
-	 * key = Name of the style (currently the directory name)
-	 * value = Path to the style
-	 */
-	typedef QHash<QString, QString> StyleList;
-
-	/**
 	 * The StyleInstallStatus enum. It gives better return value for installStyle().
 	 * - StyleInstallOk : The install went fine.
 	 * - StyleNotValid : The archive didn't contain a valid Chat Window style.
@@ -84,7 +77,7 @@
 	/**
 	 * Get all available styles.
 	 */
-	StyleList getAvailableStyles();
+	QStringList getAvailableStyles() const;
 
 public slots:
 	/**
@@ -102,7 +95,7 @@
 	 * @param stylePath the path of the style to remove.
 	 * @return true if the deletion went without problems.
 	 */
-	bool removeStyle(const QString &stylePath);
+	bool removeStyle(const QString &styleName);
 	
 	/**
 	 * Get a instance of a ChatWindowStyle from the pool.
@@ -112,7 +105,7 @@
 	 * @param stylePath Path for the specified style. Name can be ambigous.
 	 * @return the instance of ChatWindow for the specified style. DO NOT DELETE IT.
 	 */
-	ChatWindowStyle *getStyleFromPool(const QString &stylePath);
+	ChatWindowStyle *getStyleFromPool(const QString &styleName);
 
 signals:
 	/**
Index: kopete/chatwindow/kopetechatwindowstyle.cpp
===================================================================
--- kopete/chatwindow/kopetechatwindowstyle.cpp	(revision 695906)
+++ kopete/chatwindow/kopetechatwindowstyle.cpp	(working copy)
@@ -25,12 +25,12 @@
 
 // KDE includes
 #include <kdebug.h>
+#include <kstandarddirs.h>
 
-
 class ChatWindowStyle::Private
 {
 public:
-	QString stylePath;
+	QString styleName;
 	StyleVariants variantsList;
 	QString baseHref;
 	QString currentVariantPath;
@@ -46,23 +46,32 @@
 	QString actionOutgoingHtml;
 };
 
-ChatWindowStyle::ChatWindowStyle(const QString &stylePath, StyleBuildMode styleBuildMode)
+ChatWindowStyle::ChatWindowStyle(const QString &styleName, StyleBuildMode styleBuildMode)
 	: d(new Private)
 {
-	init(stylePath, styleBuildMode);
+	init(styleName, styleBuildMode);
 }
 
-ChatWindowStyle::ChatWindowStyle(const QString &stylePath, const QString &variantPath, StyleBuildMode styleBuildMode)
+ChatWindowStyle::ChatWindowStyle(const QString &styleName, const QString &variantPath, StyleBuildMode styleBuildMode)
 	: d(new Private)
 {
 	d->currentVariantPath = variantPath;
-	init(stylePath, styleBuildMode);
+	init(styleName, styleBuildMode);
 }
 
-void ChatWindowStyle::init(const QString &stylePath, StyleBuildMode styleBuildMode)
+void ChatWindowStyle::init(const QString &styleName, StyleBuildMode styleBuildMode)
 {
-	d->stylePath = stylePath;
-	d->baseHref = stylePath + QString::fromUtf8("/Contents/Resources/");
+	QStringList styleDirs = KGlobal::dirs()->findDirs("appdata", QString("styles/%1/Contents/Resources/").arg(styleName));
+	if(styleDirs.isEmpty())
+	{
+		kDebug(14000) << k_funcinfo << "Failed to find style" << styleName;
+		return;
+	}
+	d->styleName = styleName;
+	if(styleDirs.count() > 1)
+		kDebug(14000) << k_funcinfo << "found several styles with the same name. using first";
+	d->baseHref = styleDirs.at(0);
+	kDebug(14000) << k_funcinfo << "Using style:" << d->baseHref;
 	readStyleFiles();
 	if(styleBuildMode & StyleBuildNormal)
 	{
@@ -86,9 +95,9 @@
 	return d->variantsList;
 }
 
-QString ChatWindowStyle::getStylePath() const
+QString ChatWindowStyle::getStyleName() const
 {
-	return d->stylePath;
+	return d->styleName;
 }
 
 QString ChatWindowStyle::getStyleBaseHref() const
Index: kopete/chatwindow/chatmessagepart.cpp
===================================================================
--- kopete/chatwindow/chatmessagepart.cpp	(revision 695906)
+++ kopete/chatwindow/chatmessagepart.cpp	(working copy)
@@ -199,9 +199,9 @@
 	d->manager = mgr;
 
 	d->currentChatStyle = ChatWindowStyleManager::self()->getStyleFromPool(
-			 KopeteChatWindowSettings::self()->stylePath() );
+			 KopeteChatWindowSettings::self()->styleName() );
 
-	kDebug(14000) << k_funcinfo << d->currentChatStyle->getStylePath();
+	kDebug(14000) << k_funcinfo << d->currentChatStyle->getStyleName();
 
 	//Security settings, we don't need this stuff
 	setJScriptEnabled( false ) ;
@@ -350,10 +350,10 @@
 	d->rtfOverride = Kopete::AppearanceSettings::self()->chatRtfOverride();
 }
 
-void ChatMessagePart::setStyle( const QString &stylePath )
+void ChatMessagePart::setStyle( const QString &styleName )
 {
 	// Create a new ChatWindowStyle
-	d->currentChatStyle = ChatWindowStyleManager::self()->getStyleFromPool(stylePath);
+	d->currentChatStyle = ChatWindowStyleManager::self()->getStyleFromPool(styleName);
 
 	// Do the actual style switch
 	// Wait for the event loop before switching the style
Index: kopete/chatwindow/kopetechatwindowstylemanager.cpp
===================================================================
--- kopete/chatwindow/kopetechatwindowstylemanager.cpp	(revision 695906)
+++ kopete/chatwindow/kopetechatwindowstylemanager.cpp	(working copy)
@@ -56,9 +56,9 @@
 	}
 
 	KDirLister *styleDirLister;
-	StyleList availableStyles;
-	
-	// key = style path, value = ChatWindowStyle instance
+	QStringList availableStyles;
+
+	// key = style name, value = ChatWindowStyle instance
 	QHash<QString, ChatWindowStyle*> stylePool;
 
 	QStack<KUrl> styleDirs;
@@ -113,7 +113,7 @@
 		d->styleDirLister->openUrl(d->styleDirs.pop(), true);
 }
 
-ChatWindowStyleManager::StyleList ChatWindowStyleManager::getAvailableStyles()
+QStringList ChatWindowStyleManager::getAvailableStyles() const
 {
 	return d->availableStyles;
 }
@@ -279,29 +279,41 @@
 	return StyleUnknow;
 }
 
-bool ChatWindowStyleManager::removeStyle(const QString &stylePath)
+bool ChatWindowStyleManager::removeStyle(const QString &styleName)
 {
-	kDebug(14000) << k_funcinfo << stylePath;
+	kDebug(14000) << k_funcinfo << styleName;
 	// Find for the current style in avaiableStyles map.
-        KUrl urlStyle(stylePath);
-        QString styleName=urlStyle.fileName();
-        StyleList::Iterator foundStyle = d->availableStyles.find(styleName);
+	int foundStyleIdx = d->availableStyles.indexOf(styleName);
 
-	// QHash iterator return end() if it found no item.
-	if(foundStyle != d->availableStyles.end())
+	if(foundStyleIdx != -1)
 	{
-		d->availableStyles.remove(*foundStyle);
-		
+		d->availableStyles.removeAt(foundStyleIdx);
+
 		// Remove and delete style from pool if needed.
-		if( d->stylePool.contains(stylePath) )
+		if( d->stylePool.contains(styleName) )
 		{
-			ChatWindowStyle *deletedStyle = d->stylePool[stylePath];
-			d->stylePool.remove(stylePath);
+			ChatWindowStyle *deletedStyle = d->stylePool[styleName];
+			d->stylePool.remove(styleName);
 			delete deletedStyle;
 		}
 	
+		QStringList styleDirs = KGlobal::dirs()->findDirs("appdata", QString("styles/%1").arg(styleName));
+		if(styleDirs.isEmpty())
+		{
+			kDebug(14000) << k_funcinfo << "Failed to find style" << styleName;
+			return false;
+		}
+
+		// attempt to delete all dirs with this style
+		int numDeleted = 0;
+		foreach( const QString& stylePath, styleDirs )
+		{
+		KUrl urlStyle(stylePath);
 		// Do the actual deletion of the directory style.
-		return KIO::NetAccess::del( urlStyle, 0 );
+		if(KIO::NetAccess::del( urlStyle, 0 ))
+			numDeleted++;
+		}
+		return numDeleted == styleDirs.count();
 	}
 	else
 	{
@@ -309,12 +321,12 @@
 	}
 }
 
-ChatWindowStyle *ChatWindowStyleManager::getStyleFromPool(const QString &stylePath)
+ChatWindowStyle *ChatWindowStyleManager::getStyleFromPool(const QString &styleName)
 {
-	if( d->stylePool.contains(stylePath) )
+	if( d->stylePool.contains(styleName) )
 	{
-		kDebug(14000) << k_funcinfo << stylePath << " was on the pool";
-		
+		kDebug(14000) << k_funcinfo << styleName << " was on the pool";
+
 		// NOTE: This is a hidden config switch for style developers
 		// Check in the config if the cache is disabled.
 		// if the cache is disabled, reload the style every time it's getted.
@@ -322,22 +334,22 @@
 		bool disableCache = config.readEntry("disableStyleCache", false);
 		if(disableCache)
 		{
-			d->stylePool[stylePath]->reload();
+			d->stylePool[styleName]->reload();
 		}
 
-		return d->stylePool[stylePath];
+		return d->stylePool[styleName];
 	}
 	else
 	{
 		// Build a chat window style and list its variants, then add it to the pool.
-		ChatWindowStyle *style = new ChatWindowStyle(stylePath, ChatWindowStyle::StyleBuildNormal);
-		d->stylePool.insert(stylePath, style);
-		
-		kDebug(14000) << k_funcinfo << stylePath << " is just created";
+		ChatWindowStyle *style = new ChatWindowStyle(styleName, ChatWindowStyle::StyleBuildNormal);
+		d->stylePool.insert(styleName, style);
 
+		kDebug(14000) << k_funcinfo << styleName << " is just created";
+
 		return style;
 	}
-	
+
 	return 0;
 }
 
@@ -351,20 +363,21 @@
 			kDebug(14000) << k_funcinfo << "Listing: " << item->url().fileName();
 			// If the style path is already in the pool, that's mean the style was updated on disk
 			// Reload the style
-			if( d->stylePool.contains(item->url().path()) )
+			QString styleName = item->url().fileName();
+			if( d->stylePool.contains(styleName) )
 			{
-				kDebug(14000) << k_funcinfo << "Updating style: " << item->url().path();
+				kDebug(14000) << k_funcinfo << "Updating style: " << styleName;
 
-				d->stylePool[item->url().path()]->reload();
+				d->stylePool[styleName]->reload();
 
 				// Add to avaialble if required.
-				if( !d->availableStyles.contains(item->url().fileName()) )
-					d->availableStyles.insert(item->url().fileName(), item->url().path());
+				if( d->availableStyles.indexOf(styleName) == -1 )
+					d->availableStyles.append(styleName);
 			}
 			else
 			{
 				// TODO: Use name from Info.plist
-				d->availableStyles.insert(item->url().fileName(), item->url().path());
+				d->availableStyles.append(styleName);
 			}
 		}
 	}
Index: kopete/chatwindow/kopetechatwindowstyle.h
===================================================================
--- kopete/chatwindow/kopetechatwindowstyle.h	(revision 695906)
+++ kopete/chatwindow/kopetechatwindowstyle.h	(working copy)
@@ -47,10 +47,10 @@
 
 	/**
 	 * @brief Build a single chat window style.
-	 * 
+	 *
 	 */
-	explicit ChatWindowStyle(const QString &stylePath, StyleBuildMode styleBuildMode = StyleBuildNormal);
-	ChatWindowStyle(const QString &stylePath, const QString &variantPath, StyleBuildMode styleBuildMode = StyleBuildFast);
+	explicit ChatWindowStyle(const QString &styleName, StyleBuildMode styleBuildMode = StyleBuildNormal);
+	ChatWindowStyle(const QString &styleName, const QString &variantPath, StyleBuildMode styleBuildMode = StyleBuildFast);
 	~ChatWindowStyle();
 
 	/**
@@ -70,7 +70,7 @@
 	 *
 	 * @return the style path based.
 	 */
-	QString getStylePath() const;
+	QString getStyleName() const;
 
 	/**
 	 * Get the style ressource directory.
@@ -115,7 +115,7 @@
 	/**
 	 * Init this class
 	 */
-	void init(const QString &stylePath, StyleBuildMode styleBuildMode);
+	void init(const QString &styleName, StyleBuildMode styleBuildMode);
 
 	/**
 	 * List available variants for the current style.
Index: kopete/chatwindow/chatmessagepart.h
===================================================================
--- kopete/chatwindow/chatmessagepart.h	(revision 695906)
+++ kopete/chatwindow/chatmessagepart.h	(working copy)
@@ -106,7 +106,7 @@
 	 *
 	 * @param stylePath absolute path to the style.
 	 */
-	void setStyle( const QString &stylePath );
+	void setStyle( const QString &styleName );
 	
 	/**
 	 * Change the current style
Index: kopete/chatwindow/kopetechatwindowsettings.kcfg
===================================================================
--- kopete/chatwindow/kopetechatwindowsettings.kcfg	(revision 695906)
+++ kopete/chatwindow/kopetechatwindowsettings.kcfg	(working copy)
@@ -6,7 +6,6 @@
       xsi:schemaLocation="http://www.kde.org/standards/kcfg/1.0
       http://www.kde.org/standards/kcfg/1.0/kcfg.xsd"; >
 	<kcfgfile name="kopeterc"/>
-	<include>kstandarddirs.h</include>
 	<include>kglobalsettings.h</include>
 
 	<!-- Define the available signals to emit -->
@@ -18,7 +17,7 @@
 	</signal>
 	<signal name="styleChanged">
 		<label>Emited when the chat window style change.</label>
-		<argument type="String">stylePath</argument>
+		<argument type="String">styleName</argument>
 	</signal>
 	<signal name="styleVariantChanged">
 		<label>Emitted when ChatWindowStyle variant has changed.</label>
@@ -26,11 +25,10 @@
 	</signal>
 
 	<group name="Appearance">
-		
 		<!-- Chat Window Style preferences -->
-		<entry key="stylePath" type="String">
-			<label>Absolute path to a directory containing a Adium/Kopete chat window style.</label>
-			<default code="true">KStandardDirs::locate("appdata", QString("styles/%1/Kopete"))</default>
+		<entry key="styleName" type="String">
+			<label>Name of a Adium/Kopete chat window style.</label>
+			<default>Kopete</default>
 		</entry>
 		<entry key="styleVariant" type="String">
 			<label>Relative path to a CSS variant for the current style.</label>
_______________________________________________
kopete-devel mailing list
[email protected]
https://mail.kde.org/mailman/listinfo/kopete-devel

Reply via email to