• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdenetwork API Reference
  • KDE Home
  • Contact Us
 

kopete/kopete

  • sources
  • kde-4.14
  • kdenetwork
  • kopete
  • kopete
  • contactlist
contactlistlayoutmanager.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (c) 2008 Nikolaj Hald Nielsen <nhnFreespirit@gmail.com> *
3  * Copyright (c) 2009 Roman Jarosz <kedgedev@gmail.com> *
4  * *
5  * This program is free software; you can redistribute it and/or modify *
6  * it under the terms of the GNU General Public License as published by *
7  * the Free Software Foundation; either version 2 of the License, or *
8  * (at your option) any later version. *
9  * *
10  * This program is distributed in the hope that it will be useful, *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13  * GNU General Public License for more details. *
14  * *
15  * You should have received a copy of the GNU General Public License *
16  * along with this program; if not, write to the *
17  * Free Software Foundation, Inc., *
18  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
19  ***************************************************************************/
20 
21 #include "contactlistlayoutmanager.h"
22 
23 #include <KMessageBox>
24 #include <KStandardDirs>
25 #include <KUrl>
26 #include <KGlobal>
27 #include <KConfig>
28 #include <KConfigGroup>
29 #include <KDebug>
30 #include <KLocale>
31 #include <KStandardDirs>
32 #include <kio/global.h>
33 
34 #include <QDir>
35 #include <QDomDocument>
36 #include <QFile>
37 #include <QStringList>
38 
39 #include "kopeteitembase.h"
40 
41 namespace ContactList {
42 
43 LayoutManager * LayoutManager::s_instance = 0;
44 
45 const QString DefaultStyleName = "Default";
46 
47 LayoutManager * LayoutManager::instance()
48 {
49  if ( s_instance == 0 )
50  s_instance = new LayoutManager();
51 
52  return s_instance;
53 }
54 
55 LayoutManager::LayoutManager()
56  : QObject()
57 {
58  m_tokens << ContactListTokenConfig( -1, "Placeholder", i18n("Placeholder"), "transform-move" );
59  m_tokens << ContactListTokenConfig( Qt::DisplayRole, "DisplayName", i18n("Display Name"), "user-identity" );
60  m_tokens << ContactListTokenConfig( Kopete::Items::StatusTitleRole, "StatusTitle", i18n("Status Title"), "im-status-message-edit" );
61  m_tokens << ContactListTokenConfig( Kopete::Items::StatusMessageRole, "StatusMessage", i18n("Status Message"), "im-status-message-edit" );
62  m_tokens << ContactListTokenConfig( -1, "ContactIcons", i18n("Contact Icons"), "user-online" );
63 
64  loadDefaultLayouts();
65  loadUserLayouts();
66 
67  KConfigGroup config( KGlobal::config(), "ContactList Layout" );
68  m_activeLayout = config.readEntry( "CurrentLayout", DefaultStyleName );
69 
70  // Fallback to default
71  if ( !m_layouts.contains( m_activeLayout ) )
72  setActiveLayout( DefaultStyleName );
73 }
74 
75 LayoutManager::~LayoutManager()
76 {}
77 
78 QStringList LayoutManager::layouts() const
79 {
80  return m_layouts.keys();
81 }
82 
83 void LayoutManager::setActiveLayout( const QString &layout )
84 {
85  kDebug() << layout;
86  m_activeLayout = layout;
87  KConfigGroup config(KGlobal::config(), "ContactList Layout");
88  config.writeEntry( "CurrentLayout", m_activeLayout );
89  emit( activeLayoutChanged() );
90 }
91 
92 void LayoutManager::setPreviewLayout( const ContactListLayout &layout )
93 {
94  m_activeLayout = "%%PREVIEW%%";
95  m_previewLayout = layout;
96  emit( activeLayoutChanged() );
97 }
98 
99 ContactListLayout LayoutManager::activeLayout()
100 {
101  if ( m_activeLayout == "%%PREVIEW%%" )
102  return m_previewLayout;
103  return m_layouts.value( m_activeLayout );
104 }
105 
106 void LayoutManager::loadUserLayouts()
107 {
108  QDir layoutsDir = QDir( KStandardDirs::locateLocal( "appdata", QString::fromUtf8("contactlistlayouts") ) );
109 
110  layoutsDir.setSorting( QDir::Name );
111 
112  QStringList filters;
113  filters << "*.xml" << "*.XML";
114  layoutsDir.setNameFilters(filters);
115  layoutsDir.setSorting( QDir::Name );
116 
117  QFileInfoList list = layoutsDir.entryInfoList();
118 
119  for ( int i = 0; i < list.size(); ++i )
120  {
121  QFileInfo fileInfo = list.at(i);
122  kDebug() << "found user file: " << fileInfo.fileName();
123  loadLayouts( layoutsDir.filePath( fileInfo.fileName() ), true );
124  }
125 }
126 
127 void LayoutManager::loadDefaultLayouts()
128 {
129  loadLayouts( KStandardDirs::locate( "data", "kopete/DefaultContactListLayouts.xml" ), false );
130  loadLayouts( KStandardDirs::locate( "data", "kopete/CompactContactListLayouts.xml" ), false );
131 }
132 
133 
134 void LayoutManager::loadLayouts( const QString &fileName, bool user )
135 {
136  QDomDocument doc( "layouts" );
137  if ( !QFile::exists( fileName ) )
138  {
139  kDebug() << "file " << fileName << "does not exist";
140  return;
141  }
142 
143  QFile file( fileName );
144  if( !file.open( QIODevice::ReadOnly ) )
145  {
146  kDebug() << "error reading file " << fileName;
147  return;
148  }
149  if ( !doc.setContent( &file ) )
150  {
151  kDebug() << "error parsing file " << fileName;
152  return;
153  }
154  file.close();
155 
156  QDomElement layouts_element = doc.firstChildElement( "contactlist_layouts" );
157  QDomNodeList layouts = layouts_element.elementsByTagName("layout");
158 
159  int index = 0;
160  while ( index < layouts.size() )
161  {
162  QDomNode layout = layouts.item( index );
163  index++;
164 
165  QString layoutName = layout.toElement().attribute( "name", "" );
166  ContactListLayout currentLayout;
167  currentLayout.setIsEditable( user );
168 
169  currentLayout.setLayout( parseItemConfig( layout.toElement() ) );
170 
171  if ( !layoutName.isEmpty() )
172  m_layouts.insert( layoutName, currentLayout );
173  }
174 }
175 
176 LayoutItemConfig LayoutManager::parseItemConfig( const QDomElement &elem )
177 {
178  const bool showMetaContactIcon = ( elem.attribute( "show_metacontact_icon", "false" ).compare( "true", Qt::CaseInsensitive ) == 0 );
179 
180  LayoutItemConfig config;
181  config.setShowIcon( showMetaContactIcon );
182 
183  QDomNodeList rows = elem.elementsByTagName("row");
184 
185  int index = 0;
186  while ( index < rows.size() )
187  {
188  QDomNode rowNode = rows.item( index );
189  index++;
190 
191  LayoutItemConfigRow row;
192 
193  QDomNodeList elements = rowNode.toElement().elementsByTagName("element");
194 
195  int index2 = 0;
196  while ( index2 < elements.size() )
197  {
198  QDomNode elementNode = elements.item( index2 );
199  index2++;
200 
201  int value = 0; // Placeholder as default
202  QString configName = elementNode.toElement().attribute( "value" );
203  if ( !configName.isEmpty() )
204  {
205  for ( int i = 0; i < m_tokens.size(); i++)
206  {
207  if ( m_tokens.at(i).mConfigName == configName )
208  {
209  value = i;
210  break;
211  }
212  }
213  }
214 
215 
216  QString prefix = elementNode.toElement().attribute( "prefix", QString() );
217  QString sufix = elementNode.toElement().attribute( "suffix", QString() );
218  qreal size = elementNode.toElement().attribute( "size", "0.0" ).toDouble();
219  bool bold = ( elementNode.toElement().attribute( "bold", "false" ).compare( "true", Qt::CaseInsensitive ) == 0 );
220  bool italic = ( elementNode.toElement().attribute( "italic", "false" ).compare( "true", Qt::CaseInsensitive ) == 0 );
221  bool small = ( elementNode.toElement().attribute( "small", "false" ).compare( "true", Qt::CaseInsensitive ) == 0 );
222  bool optimalSize = ( elementNode.toElement().attribute( "optimalSize", "false" ).compare( "true", Qt::CaseInsensitive ) == 0 );
223  QString alignmentString = elementNode.toElement().attribute( "alignment", "left" );
224  Qt::Alignment alignment;
225 
226 
227  if ( alignmentString.compare( "left", Qt::CaseInsensitive ) == 0 )
228  alignment = Qt::AlignLeft | Qt::AlignVCenter;
229  else if ( alignmentString.compare( "right", Qt::CaseInsensitive ) == 0 )
230  alignment = Qt::AlignRight| Qt::AlignVCenter;
231  else
232  alignment = Qt::AlignCenter| Qt::AlignVCenter;
233 
234  row.addElement( LayoutItemConfigRowElement( value, size, bold, italic, small, optimalSize, alignment, prefix, sufix ) );
235  }
236 
237  config.addRow( row );
238  }
239 
240  return config;
241 }
242 
243 ContactListLayout LayoutManager::layout( const QString &layout )
244 {
245  return m_layouts.value( layout );
246 }
247 
248 bool LayoutManager::addUserLayout( const QString &name, ContactListLayout layout )
249 {
250  layout.setIsEditable( true );
251 
252  QDomDocument doc( "layouts" );
253  QDomElement layouts_element = doc.createElement( "contactlist_layouts" );
254  QDomElement newLayout = createItemElement( doc, "layout", layout.layout() );
255  newLayout.setAttribute( "name", name );
256 
257  doc.appendChild( layouts_element );
258  layouts_element.appendChild( newLayout );
259 
260  QString dirName = KStandardDirs::locateLocal( "appdata", QString::fromUtf8("contactlistlayouts") );
261  QDir layoutsDir = QDir( dirName );
262 
263  //make sure that this dir exists
264  if ( !layoutsDir.exists() )
265  {
266  if ( !layoutsDir.mkpath( dirName ) )
267  {
268  KMessageBox::sorry( 0, KIO::buildErrorString( KIO::ERR_COULD_NOT_MKDIR, dirName ) );
269  return false;
270  }
271  }
272 
273  QFile file( layoutsDir.filePath( name + ".xml" ) );
274  if ( !file.open(QIODevice::WriteOnly | QIODevice::Text) )
275  {
276  KMessageBox::sorry( 0, KIO::buildErrorString( KIO::ERR_CANNOT_OPEN_FOR_WRITING, file.fileName() ) );
277  return false;
278  }
279 
280  QTextStream out( &file );
281  out << doc.toString();
282  file.close();
283 
284  m_layouts.insert( name, layout );
285  emit( layoutListChanged() );
286  return true;
287 }
288 
289 QDomElement LayoutManager::createItemElement( QDomDocument doc, const QString &name, const LayoutItemConfig & item ) const
290 {
291  QDomElement element = doc.createElement( name );
292 
293  QString showIcon = item.showIcon() ? "true" : "false";
294  element.setAttribute ( "show_metacontact_icon", showIcon );
295 
296  for( int i = 0; i < item.rows(); i++ )
297  {
298  LayoutItemConfigRow row = item.row( i );
299 
300  QDomElement rowElement = doc.createElement( "row" );
301  element.appendChild( rowElement );
302 
303  for( int j = 0; j < row.count(); j++ ) {
304  LayoutItemConfigRowElement element = row.element( j );
305  QDomElement elementElement = doc.createElement( "element" );
306 
307  elementElement.setAttribute ( "value", m_tokens.at(element.value()).mConfigName );
308  elementElement.setAttribute ( "size", QString::number( element.size() ) );
309  elementElement.setAttribute ( "bold", element.bold() ? "true" : "false" );
310  elementElement.setAttribute ( "italic", element.italic() ? "true" : "false" );
311  elementElement.setAttribute ( "small", element.small() ? "true" : "false" );
312  elementElement.setAttribute ( "optimalSize", element.optimalSize() ? "true" : "false" );
313 
314  QString alignmentString;
315  if ( element.alignment() & Qt::AlignLeft )
316  alignmentString = "left";
317  else if ( element.alignment() & Qt::AlignRight )
318  alignmentString = "right";
319  else
320  alignmentString = "center";
321 
322  elementElement.setAttribute ( "alignment", alignmentString );
323 
324  rowElement.appendChild( elementElement );
325  }
326  }
327 
328  return element;
329 }
330 
331 bool LayoutManager::isDefaultLayout( const QString & layout ) const
332 {
333  if ( m_layouts.keys().contains( layout ) )
334  return !m_layouts.value( layout ).isEditable();
335 
336  return false;
337 }
338 
339 QString LayoutManager::activeLayoutName() const
340 {
341  return m_activeLayout;
342 }
343 
344 bool LayoutManager::deleteLayout( const QString & layout )
345 {
346  //check if layout is editable
347  if ( m_layouts.value( layout ).isEditable() )
348  {
349  QDir layoutsDir = QDir( KStandardDirs::locateLocal( "appdata", QString::fromUtf8("contactlistlayouts") ) );
350  QString xmlFile = layoutsDir.path() + '/' + layout + ".xml";
351  kDebug() << "deleting file: " << xmlFile;
352 
353  if ( !QFile::remove( xmlFile ) )
354  {
355  KMessageBox::sorry( 0, KIO::buildErrorString( KIO::ERR_CANNOT_DELETE, xmlFile ) );
356  return false;
357  }
358 
359  m_layouts.remove( layout );
360  emit( layoutListChanged() );
361 
362  if ( layout == m_activeLayout )
363  setActiveLayout( DefaultStyleName );
364 
365  return true;
366  }
367 
368  KMessageBox::sorry( 0, i18n( "The layout '%1' is one of the default layouts and cannot be deleted.", layout ),
369  i18n( "Cannot Delete Default Layouts" ) );
370  return false;
371 }
372 
373 } //namespace ContactList
374 
375 #include "contactlistlayoutmanager.moc"
QDomElement::elementsByTagName
QDomNodeList elementsByTagName(const QString &tagname) const
ContactList::LayoutItemConfigRowElement::alignment
Qt::Alignment alignment() const
Definition: contactlistlayoutitemconfig.h:42
ContactList::LayoutItemConfig::row
LayoutItemConfigRow row(int at) const
Definition: contactlistlayoutitemconfig.cpp:74
QDomNodeList::item
QDomNode item(int index) const
ContactList::LayoutManager::layouts
QStringList layouts() const
Definition: contactlistlayoutmanager.cpp:78
QDir::setNameFilters
void setNameFilters(const QStringList &nameFilters)
ContactList::LayoutItemConfigRowElement::small
bool small() const
Definition: contactlistlayoutitemconfig.h:45
ContactList::ContactListLayout::setIsEditable
void setIsEditable(bool editable)
Definition: contactlistlayoutitemconfig.cpp:112
QDomNode::appendChild
QDomNode appendChild(const QDomNode &newChild)
kopeteitembase.h
Contains definitions common between model items.
contactlistlayoutmanager.h
QDomElement::attribute
QString attribute(const QString &name, const QString &defValue) const
ContactList::LayoutItemConfigRowElement::optimalSize
bool optimalSize() const
Definition: contactlistlayoutitemconfig.h:46
QFile::remove
bool remove()
QDomDocument::toString
QString toString(int indent) const
ContactList::LayoutItemConfigRowElement::size
qreal size() const
Definition: contactlistlayoutitemconfig.h:39
ContactList::LayoutManager::setActiveLayout
void setActiveLayout(const QString &layout)
Definition: contactlistlayoutmanager.cpp:83
QDomNodeList
QDir::filePath
QString filePath(const QString &fileName) const
ContactList::DefaultStyleName
const QString DefaultStyleName
Definition: contactlistlayoutmanager.cpp:45
ContactList::LayoutItemConfig::rows
int rows() const
Definition: contactlistlayoutitemconfig.cpp:68
ContactList::LayoutManager::addUserLayout
bool addUserLayout(const QString &name, ContactListLayout layout)
Definition: contactlistlayoutmanager.cpp:248
QDomNode
QFile::exists
bool exists() const
Qt::Alignment
typedef Alignment
QString::toDouble
double toDouble(bool *ok) const
QDir::setSorting
void setSorting(QFlags< QDir::SortFlag > sort)
QFile
QTextStream
ContactList::LayoutItemConfig
This class wraps the data needed to paint a LayoutItemDelegate.
Definition: contactlistlayoutitemconfig.h:73
ContactList::LayoutItemConfigRow
Definition: contactlistlayoutitemconfig.h:59
QDomNode::toElement
QDomElement toElement() const
ContactList::ContactListLayout
Definition: contactlistlayoutitemconfig.h:92
ContactList::LayoutItemConfig::showIcon
bool showIcon() const
Definition: contactlistlayoutitemconfig.cpp:84
QString::number
QString number(int n, int base)
QDir::exists
bool exists() const
Kopete::Items::StatusMessageRole
const int StatusMessageRole
Definition: kopeteitembase.h:48
QString::fromUtf8
QString fromUtf8(const char *str, int size)
QDir::path
QString path() const
QFileInfo::fileName
QString fileName() const
ContactList::LayoutManager::instance
static LayoutManager * instance()
Definition: contactlistlayoutmanager.cpp:47
QObject
ContactList::LayoutManager::isDefaultLayout
bool isDefaultLayout(const QString &layout) const
Definition: contactlistlayoutmanager.cpp:331
QDomElement::setAttribute
void setAttribute(const QString &name, const QString &value)
QDir::entryInfoList
QFileInfoList entryInfoList(QFlags< QDir::Filter > filters, QFlags< QDir::SortFlag > sort) const
QString::isEmpty
bool isEmpty() const
ContactList::LayoutManager::activeLayoutName
QString activeLayoutName() const
Definition: contactlistlayoutmanager.cpp:339
ContactList::LayoutItemConfigRow::count
int count() const
Definition: contactlistlayoutitemconfig.cpp:46
QString
ContactList::ContactListTokenConfig
Definition: contactlistlayoutmanager.h:37
QStringList
QFileInfo
QDomDocument
QDir
ContactList::ContactListLayout::layout
LayoutItemConfig layout() const
Definition: contactlistlayoutitemconfig.cpp:97
ContactList::LayoutManager
Definition: contactlistlayoutmanager.h:53
ContactList::LayoutItemConfigRow::element
LayoutItemConfigRowElement element(int at) const
Definition: contactlistlayoutitemconfig.cpp:51
ContactList::LayoutItemConfigRowElement::bold
bool bold() const
Definition: contactlistlayoutitemconfig.h:40
ContactList::LayoutManager::deleteLayout
bool deleteLayout(const QString &layout)
Definition: contactlistlayoutmanager.cpp:344
ContactList::LayoutItemConfigRowElement::italic
bool italic() const
Definition: contactlistlayoutitemconfig.h:41
ContactList::LayoutManager::activeLayout
ContactListLayout activeLayout()
Definition: contactlistlayoutmanager.cpp:99
QDomNode::firstChildElement
QDomElement firstChildElement(const QString &tagName) const
ContactList::LayoutItemConfigRowElement
Definition: contactlistlayoutitemconfig.h:31
ContactList::LayoutManager::setPreviewLayout
void setPreviewLayout(const ContactListLayout &layout)
Definition: contactlistlayoutmanager.cpp:92
ContactList::LayoutManager::layout
ContactListLayout layout(const QString &layout)
Definition: contactlistlayoutmanager.cpp:243
QDomNodeList::size
int size() const
QDomDocument::createElement
QDomElement createElement(const QString &tagName)
ContactList::LayoutItemConfigRowElement::value
int value() const
Definition: contactlistlayoutitemconfig.h:38
QDomElement
QString::compare
int compare(const QString &other) const
ContactList::LayoutManager::activeLayoutChanged
void activeLayoutChanged()
ContactList::LayoutManager::layoutListChanged
void layoutListChanged()
Kopete::Items::StatusTitleRole
const int StatusTitleRole
Definition: kopeteitembase.h:47
QDir::mkpath
bool mkpath(const QString &dirPath) const
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:29:08 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

kopete/kopete

Skip menu "kopete/kopete"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdenetwork API Reference

Skip menu "kdenetwork API Reference"
  • kget
  • kopete
  •   kopete
  •   libkopete
  • krdc
  • krfb

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal