Show
Ignore:
Timestamp:
11/30/06 03:02:49 (2 years ago)
Author:
erijo
Message:

Patch from Anders Olofsson to make the history layout configurable. Also adds a new style format that can be used in the history. Fixes part of #1449.

With this commit, <font> tags are removed before the message is displayed. This means that the sender can no longer choose the font or color to show the message in. IMHO, this is a good thing. But if you have an other opinion, speak up and maybe I'll make it configurable.

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • trunk/qt-gui/src/ewidgets.cpp

    r4788 r4791  
    4545#include <qlayout.h> 
    4646#include <qheader.h> 
     47#include <qstringlist.h> 
    4748#include <ctype.h> 
    4849#include <algorithm> 
     
    662663 
    663664//- Message View Widget --------------------------------------------------------- 
     665 
     666QStringList CMessageViewWidget::getStyleNames(bool includeHistoryStyles) 
     667{ 
     668  static const char *const styleNames[] = { 
     669    QT_TR_NOOP("Default"), 
     670    QT_TR_NOOP("Compact"), 
     671    QT_TR_NOOP("Tiny"), 
     672    QT_TR_NOOP("Table"), 
     673    QT_TR_NOOP("Long"), 
     674    QT_TR_NOOP("Wide") 
     675  }; 
     676 
     677  int listLength; 
     678 
     679  // Style 5 (Wide) is currently only supported in buffered mode which is not used for chat 
     680  if(includeHistoryStyles) 
     681    listLength = 6; 
     682  else 
     683    listLength = 5; 
     684 
     685  QStringList styleList; 
     686  for(int i = 0; i < listLength; ++i) 
     687    styleList.append(tr(styleNames[i])); 
     688 
     689  return styleList; 
     690} 
     691 
    664692CMessageViewWidget::CMessageViewWidget(const char *szId, unsigned long nPPID, 
    665   CMainWindow *m, QWidget* parent, const char * name) 
     693  CMainWindow *m, QWidget* parent, const char *name, bool historyMode) 
    666694  : CHistoryWidget(parent, name) 
    667695{ 
    668696  m_szId = szId ? strdup(szId) : 0; 
    669697  m_nPPID = nPPID; 
    670   m_nMsgStyle = m->m_nMsgStyle; 
    671   m_nDateFormat = m->m_nDateFormat; 
    672   m_bAppendLineBreak = m->m_bAppendLineBreak; 
     698  if (historyMode) 
     699  { 
     700    m_useBuffer = true; 
     701    m_nMsgStyle = m->m_histMsgStyle; 
     702    m_nDateFormat = m->m_histDateFormat; 
     703    m_extraSpacing = m->m_histVertSpacing; 
     704    m_appendLineBreak = false; 
     705  } 
     706  else 
     707  { 
     708    m_useBuffer = false; 
     709    m_nMsgStyle = m->m_chatMsgStyle; 
     710    m_nDateFormat = m->m_chatDateFormat; 
     711    m_extraSpacing = m->m_chatVertSpacing; 
     712    m_appendLineBreak = m->m_chatAppendLineBreak; 
     713  } 
    673714  m_colorRcv = m->m_colorRcv; 
    674715  m_colorSnt = m->m_colorSnt; 
     
    679720  mainwin = m; 
    680721 
     722  clear(); 
     723 
    681724/* 
    682725  // add all unread messages. 
     
    698741} 
    699742 
    700 CMessageViewWidget::CMessageViewWidget(unsigned long _nUin, CMainWindow *m, QWidget* parent, const char * name) 
     743CMessageViewWidget::CMessageViewWidget(unsigned long _nUin, CMainWindow *m, QWidget *parent, const char *name, bool historyMode) 
    701744:CHistoryWidget(parent,name) 
    702745{ 
    703746  m_nUin= _nUin; 
    704747  m_szId = NULL; // avoid desalocation error at destructor 
    705   m_nMsgStyle = m->m_nMsgStyle; 
    706   m_nDateFormat = m->m_nDateFormat; 
    707   m_bAppendLineBreak = m->m_bAppendLineBreak; 
     748  if (historyMode) 
     749  { 
     750    m_useBuffer = true; 
     751    m_nMsgStyle = m->m_histMsgStyle; 
     752    m_nDateFormat = m->m_histDateFormat; 
     753    m_extraSpacing = m->m_histVertSpacing; 
     754    m_appendLineBreak = false; 
     755  } 
     756  else 
     757  { 
     758    m_useBuffer = false; 
     759    m_nMsgStyle = m->m_chatMsgStyle; 
     760    m_nDateFormat = m->m_chatDateFormat; 
     761    m_extraSpacing = m->m_chatVertSpacing; 
     762    m_appendLineBreak = m->m_chatAppendLineBreak; 
     763  } 
    708764  m_colorRcv = m->m_colorRcv; 
    709765  m_colorSnt = m->m_colorSnt; 
     
    713769  setPaletteBackgroundColor(m->m_colorChatBkg); 
    714770  mainwin = m; 
     771 
     772  clear(); 
     773 
    715774/* 
    716775  // add all unread messages. 
     
    746805} 
    747806 
     807void CMessageViewWidget::clear() 
     808{ 
     809  CHistoryWidget::clear(); 
     810 
     811  m_buffer = ""; 
     812 
     813  switch (m_nMsgStyle) 
     814  { 
     815    case 5: 
     816      // table doesn't work when appending so must buffer when using this style 
     817      m_buffer.append("<table border=\"0\">"); 
     818      m_useBuffer = true; 
     819      break; 
     820  } 
     821  if (m_useBuffer) 
     822    m_buffer.prepend("<html><body>"); 
     823} 
     824 
     825void CMessageViewWidget::updateContent() 
     826{ 
     827  if (m_useBuffer) 
     828    setText(m_buffer); 
     829} 
     830 
     831void CMessageViewWidget::internalAddMsg(QString s) 
     832{ 
     833  if (m_extraSpacing) 
     834  { 
     835    if (m_nMsgStyle != 5) 
     836    { 
     837      if (m_useBuffer) 
     838      { 
     839        s.prepend("<p>"); 
     840        s.append("</p>"); 
     841      } 
     842      else 
     843      { 
     844        s.append("<br>"); 
     845      } 
     846    } 
     847    else 
     848    { 
     849      s.append("<tr><td colspan=\"3\"></td></tr>"); 
     850    } 
     851  } 
     852 
     853  if (m_useBuffer) 
     854  { 
     855    if (!m_extraSpacing && m_nMsgStyle != 5) 
     856      s.append("<br>"); 
     857 
     858    m_buffer.append(s); 
     859    if (m_appendLineBreak) 
     860      m_buffer.append("<hr>"); 
     861  } 
     862  else 
     863  { 
     864    append(s); 
     865    if (m_appendLineBreak) 
     866      append("<hr>"); 
     867  } 
     868} 
     869 
    748870void CMessageViewWidget::addMsg(ICQEvent * _e) 
    749871{ 
     
    759881  QString s; 
    760882  QString color; 
    761   bool bAIM = (m_nPPID == LICQ_PPID) && !isdigit(m_szId[0]); 
    762  
    763   if (fromHistory)  
     883 
     884  if (fromHistory) 
     885  { 
    764886    if (dir == D_RECEIVER)  
    765887      color = m_colorRcvHistory.name(); 
    766888    else  
    767       color = m_colorSntHistory.name();   
     889      color = m_colorSntHistory.name(); 
     890  } 
    768891  else 
     892  { 
    769893    if (dir == D_RECEIVER)  
    770894      color = m_colorRcv.name(); 
    771895    else  
    772       color = m_colorSnt.name();   
    773    
    774   /* Remove trailing line breaks. */ 
    775   for (unsigned int i = messageText.length() - 1; i>=0; i--) { 
    776     if (messageText.at(i) == '\n' || messageText.at(i) == '\r') { 
     896      color = m_colorSnt.name(); 
     897  } 
     898 
     899  // Remove trailing line breaks. 
     900  for (int i = messageText.length(); i >= 0; i--) 
     901  { 
     902    if (messageText.at(i - 1) != '\n' && messageText.at(i - 1) != '\r') 
     903    { 
    777904      messageText.truncate(i); 
    778     } else { 
    779905      break; 
    780906    } 
    781907  } 
    782       
     908 
     909  // Extract everything inside <body>...</body> 
     910  // Leaving <html> and <body> messes with our message display 
     911  QRegExp body("<body[^>]*>(.*)</body>"); 
     912  if (body.search(messageText) != -1) 
     913    messageText = body.cap(1); 
     914 
     915  // Remove all font tags 
     916  messageText.replace(QRegExp("</?font[^>]*>"), ""); 
     917 
    783918  QString my_date = date.toString( m_nDateFormat ); 
    784    
     919 
    785920  switch (m_nMsgStyle) { 
    786921    case 0: 
    787       s = QString("<html><body><font color=\"%1\"><b>%2%3 [%4%5%6%7] %8:</b></font><br>") 
     922      s = QString("<font color=\"%1\"><b>%2%3 [%4%5%6%7] %8:</b></font><br>") 
    788923                  .arg(color) 
    789924                  .arg(eventDescription) 
     
    794929                  .arg(isEncrypted ? 'E' : '-') 
    795930                  .arg(contactName); 
    796       s.append(QString("<font color=\"%1\">%2</font></body></html>") 
     931      s.append(QString("<font color=\"%1\">%2</font>") 
    797932                      .arg(color) 
    798933                      .arg(messageText)); 
     
    811946                      .arg(color) 
    812947                      .arg(messageText)); 
    813       if (bAIM) 
    814       { 
    815         s.prepend("<html><body>"); 
    816         s.append("</body></html>"); 
    817       } 
    818  
    819948      break; 
    820949    case 2: 
     
    827956                      .arg(color) 
    828957                      .arg(messageText)); 
    829  
    830       if (bAIM) 
    831       { 
    832         s.prepend("<html><body>"); 
    833         s.append("</body></html>"); 
    834       } 
    835  
    836958      break;   
    837959    case 3: 
     
    864986                  .arg(color) 
    865987                  .arg(messageText)); 
    866     break;      
    867   } 
    868    
    869   append(s); 
    870   if (m_bAppendLineBreak) { 
    871     append("<hr>"); 
    872   } 
     988      break; 
     989    case 5: 
     990      // Mode 5 is a table so it cannot be displayed in paragraphs 
     991      s = QString("<tr><td><nobr><b><font color=\"%1\">%2</font><b></nobr></td>") 
     992                  .arg(color) 
     993                  .arg(my_date); 
     994      s.append(QString("<td><b><font color=\"%3\">%4</font></b></font></td>") 
     995                       .arg(color) 
     996                       .arg(contactName)); 
     997      s.append(QString("<td><font color=\"%1\">%2</font></td></tr>") 
     998                      .arg(color) 
     999                      .arg(messageText)); 
     1000      break; 
     1001  } 
     1002 
     1003  internalAddMsg(s); 
    8731004} 
    8741005 
     
    9891120      break;  
    9901121 
     1122    case 5: 
     1123      s = QString("<tr><td><b><font color=\"%1\">%2</font><b></td><td colspan=\"2\"><b><font color=\"%3\">%4</font></b></font></td></tr>") 
     1124                  .arg(color) 
     1125                  .arg(dateTime) 
     1126                  .arg(color) 
     1127                  .arg(messageText); 
     1128      break; 
     1129 
    9911130    case 0: 
    9921131    default: 
    993       s = QString("<html><body><font color=\"%1\"><b>[%2] %3</b></font><br></body></html>") 
     1132      s = QString("<font color=\"%1\"><b>[%2] %3</b></font><br>") 
    9941133                  .arg(color) 
    9951134                  .arg(dateTime) 
     
    9981137  } 
    9991138 
    1000   append(s); 
    1001   if (m_bAppendLineBreak) 
    1002     append("<hr>"); 
     1139  internalAddMsg(s); 
    10031140} 
    10041141