Hi,

> I have read brievly the patch,  here are some notes:

Thanks :)

>>  void LatexPlugin::slotMessageAboutToShow( Kopete::Message& msg )
>> {
>>+    if ( msg.direction() != Kopete::Message::Inbound )
>>+        return;
>>+
>
>Why did you disable the latex parsing of outbound message ?   Not all messages 
>can be represented as a single image,  and maybe the protocol doesn't support 
>sending images at all.

This is what I thought: When someone writes a Latex formulae it usually does it 
one formula at a time, which means only one <img ..> tag would be generated, 
then, is not necessary to process the message again because it was already 
processed in slotMessageAboutToSend, and there is no reason for the protocol to 
remove the rich text, even if it doesn't handle sending images. At least that 
was what I thought, because I think the message follows two different paths 
(sending & showing).
And right now the patch to the MSN protocol can only send one image per 
message, and the latex plugin only process one formula in 
slotMessageAboutToSend.

> anyway, message that have already been parsed should not be parsed again. We 
> need to find a better way to detect if the message has been parsed. (checking 
> for an <img/>)

Ok, I've rectified my patch, now I decide if we have to parse the message or 
not based on the presence of the <img... tag. Off course it could be something 
more complex such as searching for a $$ text $$ outside an <img /> tag, but I 
think this is enough, mainly because people tends to write one formula at a 
time.
 
>>-    config->setGroup("Latex Plugin");
>>-
>>-    if(!config->readEntry("ParseOutgoing", false))
>>-        return;
>
>I think a config option should stay

Ok, I've put it back, but I've also added an option to the plugin configuration 
dialog to select or not the outgoing parsing.

>>+    static QMap < QString, QString > latexFormulaMap;
>>+    QMap < QString, QString >::ConstIterator it = latexFormulaMap.constFind 
>>( 
>>latexFormula );
>>+    
>>+    if ( it != latexFormulaMap.constEnd () )
>>+        return it.value();
>
>maybe considering the use of QCache here.  (but i'm not sure itis required to 
>have a cache.  i even think there should not be a cache.

Ok, I've replaced that with a QCache, but surrounding the code between an #if 
CACHE and #endif, so it's easily disabled.

Thanks !!

---
Ezequiel R. Aguerre





      __________________________________________________ 
Preguntá. Respondé. Descubrí. 
Todo lo que querías saber, y lo que ni imaginabas,
está en Yahoo! Respuestas (Beta). 
¡Probalo ya! 
http://www.yahoo.com.ar/respuestas 
diff -ur ./plugins/latex/kopete_latexconvert.sh 
../../kdenetwork/kopete/plugins/latex/kopete_latexconvert.sh
--- ./plugins/latex/kopete_latexconvert.sh      2007-06-28 16:13:32.000000000 
-0300
+++ ../../kdenetwork/kopete/plugins/latex/kopete_latexconvert.sh        
2007-06-29 21:09:49.000000000 -0300
@@ -1,4 +1,4 @@
-#!/bin/sh
+#!/bin/bash
 #############################################################
 # TEX2IM:   Converts LaTeX formulas to pixel graphics which #
 #           can be easily included in Text-Processors like  #
@@ -21,7 +21,7 @@
 format="png"
 color1="white"
 color2="black"
-trans=1
+trans=0
 noformula=0
 aa=1
 extra_header="$HOME/.tex2im_header"
diff -ur ./plugins/latex/latexconfig.kcfg 
../../kdenetwork/kopete/plugins/latex/latexconfig.kcfg
--- ./plugins/latex/latexconfig.kcfg    2007-06-28 16:13:32.000000000 -0300
+++ ../../kdenetwork/kopete/plugins/latex/latexconfig.kcfg      2007-06-29 
21:27:26.000000000 -0300
@@ -15,5 +15,9 @@
                        <label>Vertical Rendering Resolution (DPI).</label>
                        <default>150</default>
                </entry>
+               <entry name="ParseOutgoing" type="UInt">
+                       <label>Parse Outgoing Messages.</label>
+                       <default>0</default>
+               </entry>
        </group>
 </kcfg>
diff -ur ./plugins/latex/latexplugin.cpp 
../../kdenetwork/kopete/plugins/latex/latexplugin.cpp
--- ./plugins/latex/latexplugin.cpp     2007-06-28 16:13:32.000000000 -0300
+++ ../../kdenetwork/kopete/plugins/latex/latexplugin.cpp       2007-06-29 
21:47:18.000000000 -0300
@@ -16,15 +16,17 @@
     *************************************************************************
 */
 
-#include <qregexp.h>
-#include <qimage.h>
-#include <qbuffer.h>
+#include <QRegExp>
+#include <QImage>
+#include <QBuffer>
 #include <QTextDocument>
+#include <QMap>
+#include <QCache>
 
 #include <kgenericfactory.h>
 #include <kdebug.h>
 #include <kstandarddirs.h>
-#include <k3process.h>
+#include <kprocess.h>
 #include <ktemporaryfile.h>
 #include <kcodecs.h>
 #include <kmessagebox.h>
@@ -37,6 +39,7 @@
 #include "latexguiclient.h"
 
 #define ENCODED_IMAGE_MODE 0
+#define CACHE 1
 
 typedef KGenericFactory<LatexPlugin> LatexPluginFactory;
 K_EXPORT_COMPONENT_FACTORY( kopete_latex, LatexPluginFactory( "kopete_latex" ) 
 )
@@ -82,9 +85,12 @@
        new LatexGUIClient( KMM );
 }
 
-
 void LatexPlugin::slotMessageAboutToShow( Kopete::Message& msg )
 {
+       // Check if the message has already been processed by looking for <img 
...> tags
+       if ( msg.escapedBody ().contains ( "<img" ) )
+               return;
+       
        QString mMagick = KStandardDirs::findExe("convert");
        if ( mMagick.isEmpty() )
        {
@@ -160,17 +166,8 @@
 
        messageText= msg.escapedBody();
 
-       int imagePxWidth,imagePxHeight;
        for (QMap<QString,QString>::ConstIterator it = replaceMap.begin(); it 
!= replaceMap.end(); ++it)
-       {
-               QImage theImage(*it);
-               if(theImage.isNull())
-                       continue;
-               imagePxWidth = theImage.width();
-               imagePxHeight = theImage.height();
-               QString 
escapedLATEX=Qt::escape(it.key()).replace("\"","&quot;");  //we need  the 
escape quotes because that string will be in a title="" argument, but not the \n
-               messageText.replace(Kopete::Message::escape(it.key()), " <img 
width=\"" + QString::number(imagePxWidth) + "\" height=\"" + 
QString::number(imagePxHeight) + "\" src=\"" + (*it) + "\"  alt=\"" + 
escapedLATEX +"\" title=\"" + escapedLATEX +"\"  /> ");
-       }
+               messageText.replace(Kopete::Message::escape(it.key()), 
it.value());
 
        msg.setHtmlBody( messageText );
 }
@@ -178,9 +175,6 @@
 
 void LatexPlugin::slotMessageAboutToSend( Kopete::Message& msg)
 {
-       Q_UNUSED(msg)
-       //disabled because to work correctly, we need to find what special has 
the gif we can send over MSN
-#if 0
        KSharedConfig::Ptr config = KGlobal::config();
        config->setGroup("Latex Plugin");
 
@@ -196,49 +190,55 @@
        // this searches for $$formula$$
        QRegExp rg("^\\s*\\$\\$([^$]+)\\$\\$\\s*$");
 
-       if( rg.search(messageText) != -1 )
+       if( rg.indexIn(messageText) != -1 )
        {
                QString latexFormula = rg.cap(1);
                if(!securityCheck( latexFormula ))
                        return;
 
-               QString url = handleLatex(latexFormula);
-
-
-               if(!url.isNull())
-               {
-                       QString escapedLATEX= 
Qt::escape(messageText).replace("\"","&quot;");
-                       QString messageText="<img src=\"" + url + "\" alt=\"" + 
escapedLATEX + "\" title=\"" + escapedLATEX +"\"  />";
-                       msg.setBody( messageText, Kopete::Message::RichText );
-               }
+               msg.setHtmlBody( handleLatex(latexFormula) );
        }
-#endif
 }
 
 QString LatexPlugin::handleLatex(const QString &latexFormula)
 {
+       #if CACHE
+       static QCache < QString, QString > cache ( 10 );
+       if ( cache.contains ( latexFormula ) )
+               return *( cache.object ( latexFormula ) );
+       #endif
+       
        KTemporaryFile *tempFile=new KTemporaryFile();
        tempFile->setPrefix("kopetelatex-");
-       tempFile->setSuffix(".png");
+       tempFile->setSuffix(".gif");
        tempFile->open();
        m_tempFiles.append(tempFile);
        QString fileName = tempFile->fileName();
 
-       K3Process p;
+       KProcess p;
                        
        QString argumentRes = "-r %1x%2";
        QString argumentOut = "-o %1";
-       //QString argumentFormat = "-fgif";  //we uses gif format because MSN 
only handle gif
+       QString argumentFormat = "-fgif";  //we uses gif format because MSN 
only handle gif
        int hDPI, vDPI;
        hDPI = LatexConfig::self()->horizontalDPI();
        vDPI = LatexConfig::self()->verticalDPI();
-       p << m_convScript <<  argumentRes.arg(QString::number(hDPI), 
QString::number(vDPI)) << argumentOut.arg(fileName) /*<< argumentFormat*/ << 
latexFormula  ;
+       p << m_convScript <<  argumentRes.arg(QString::number(hDPI), 
QString::number(vDPI)) << argumentFormat << argumentOut.arg(fileName) << 
latexFormula;
                        
        kDebug() << k_funcinfo  << " Rendering " << m_convScript << " " <<  
argumentRes.arg(QString::number(hDPI), QString::number(vDPI)) << " " << 
argumentOut.arg(fileName) << endl;
                        
        // FIXME our sucky sync filter API limitations :-)
-       p.start(K3Process::Block);
-       return fileName;
+       p.execute();
+       
+       QString escapedLATEX=Qt::escape(latexFormula).replace("\"","&quot;");
+       QString messageText="<img src=\"" + fileName + "\" alt=\"$$" + 
escapedLATEX + "$$\" title=\"$$" + escapedLATEX + "$$\" />";
+       
+       #if CACHE
+       QString *messageCache = new QString ( messageText );
+       cache.insert ( latexFormula, messageCache );
+       #endif
+
+       return messageText;
 }
 
 bool LatexPlugin::securityCheck(const QString &latexFormula)
diff -ur ./plugins/latex/latexpreferences.cpp 
../../kdenetwork/kopete/plugins/latex/latexpreferences.cpp
--- ./plugins/latex/latexpreferences.cpp        2007-06-28 16:13:32.000000000 
-0300
+++ ../../kdenetwork/kopete/plugins/latex/latexpreferences.cpp  2007-06-29 
21:26:28.000000000 -0300
@@ -46,6 +46,7 @@
        
        connect(m_preferencesDialog->horizontalDPI, SIGNAL(valueChanged(int)), 
this, SLOT(slotModified()));
        connect(m_preferencesDialog->verticalDPI, SIGNAL(valueChanged(int)), 
this, SLOT(slotModified()));
+       connect(m_preferencesDialog->parseOutgoing, SIGNAL(toggled(bool)), 
this, SLOT(slotModified()));
        
        load();
 }
@@ -61,6 +62,7 @@
        // load widgets here
        
m_preferencesDialog->horizontalDPI->setValue(LatexConfig::self()->horizontalDPI());
        
m_preferencesDialog->verticalDPI->setValue(LatexConfig::self()->verticalDPI());
+       m_preferencesDialog->parseOutgoing->setChecked ( 
LatexConfig::self()->parseOutgoing() == 1 ? true : false );
        emit KCModule::changed(false);
 }
 
@@ -73,6 +75,7 @@
 {
        
LatexConfig::self()->setHorizontalDPI(m_preferencesDialog->horizontalDPI->value());
        
LatexConfig::self()->setVerticalDPI(m_preferencesDialog->verticalDPI->value());
+       
LatexConfig::self()->setParseOutgoing(m_preferencesDialog->parseOutgoing->isChecked()
 ? 1 : 0 );
        LatexConfig::self()->writeConfig();
        emit KCModule::changed(false);
 }
diff -ur ./plugins/latex/latexprefsbase.ui 
../../kdenetwork/kopete/plugins/latex/latexprefsbase.ui
--- ./plugins/latex/latexprefsbase.ui   2007-06-28 16:13:32.000000000 -0300
+++ ../../kdenetwork/kopete/plugins/latex/latexprefsbase.ui     2007-06-29 
23:01:19.000000000 -0300
@@ -81,7 +81,14 @@
         </item>
        </layout>
       </item>
-      <item row="2" column="0" >
+      <item row="2" column="0">
+                       <widget class="QCheckBox" name="parseOutgoing">
+                               <property name="text">
+                                       <string>Parse outgoing 
messages.</string>
+                               </property>
+                       </widget>
+               </item>
+               <item row="3" column="0" >
        <spacer>
         <property name="orientation" >
          <enum>Qt::Vertical</enum>
diff -ur ./protocols/msn/msnswitchboardsocket.cpp 
../../kdenetwork/kopete/protocols/msn/msnswitchboardsocket.cpp
--- ./protocols/msn/msnswitchboardsocket.cpp    2007-06-28 16:13:40.000000000 
-0300
+++ ../../kdenetwork/kopete/protocols/msn/msnswitchboardsocket.cpp      
2007-06-28 16:36:19.000000000 -0300
@@ -667,6 +667,9 @@
 // this sends a short message to the server
 int MSNSwitchBoardSocket::sendMsg( const Kopete::Message &msg )
 {
+       bool inkSend = false;
+       QString head, inkArgs = "A", message;
+       
        if ( onlineStatus() != Connected || m_chatMembers.empty())
        {
 //             m_messagesQueue.append(msg);
@@ -699,69 +702,91 @@
 
        if( msg.format() & Qt::RichText )
        {
-               QRegExp regex("^\\s*<img src=\"([^>\"]+)\"[^>]*>\\s*$");
+               QRegExp regex("^.*<img src=\"([^>\"]+)\"[^>]*>.*$");
                if(regex.indexIn(msg.escapedBody()) != -1)
                {
-                       // FIXME why are we sending the images.. the contact 
should request them.
-                       PeerDispatcher()->sendImage(regex.cap(1), m_msgHandle);
-                       return -3;
+                       QFile imageFile(regex.cap(1));
+                       // If the image exists and is a GIF, then is suitable 
to send as an ink draw.
+                       if ( imageFile.exists() && 
imageFile.fileName().endsWith( "GIF", Qt::CaseInsensitive ) )
+                       {
+                               imageFile.open ( QIODevice::ReadOnly );
+                               QByteArray ba = imageFile.readAll();
+                               imageFile.close ();
+                               
+                               message = QString::fromLatin1 ( "base64:%1" 
).arg ( QString ( KCodecs::base64Encode ( ba ) ) );
+                               inkSend = true;
+                       }
+                       // FIXME P2P is the new and right way to go
+                       //PeerDispatcher()->sendImage(regex.cap(1), 
m_msgHandle);
+                       //return -3;
                }
        }
 
-       // User-Agent is not a official flag, but GAIM has it
-       QString UA;
-       if( config.readEntry("SendClientInfo", true) )
-       {
-               UA="User-Agent: 
Kopete/"+escape(KGlobal::mainComponent().aboutData()->version())+"\r\n";
-       }
-
-       QString head =
-               "MIME-Version: 1.0\r\n"
-               "Content-Type: text/plain; charset=UTF-8\r\n"
-               "User-Agent: 
Kopete/"+escape(KGlobal::mainComponent().aboutData()->version())+"\r\n"
-               "X-MMS-IM-Format: ";
-
-       if(msg.font() != QFont() )
-       {
-               //It's verry strange that if the font name is bigger than 31 
char, the _server_ close the socket and don't deliver the message.
-               //  the real question is why ?   my guess is that MS patched 
the server because a bug in their client,  but that's just a guess.
-               head += "FN=" + escape( msg.font().family().left(31));
-               head += "; EF=";
-               if(msg.font().bold())
-                       head += 'B';
-               if(msg.font().italic())
-                       head += 'I';
-               if(msg.font().strikeOut())
-                       head += 'S';
-               if(msg.font().underline())
-                       head += 'U';
-               head += "; ";
-       }
-       else head+="FN=; EF=; ";
-       /*
-        * I don't know what to set by default, so i decided to set nothing.  
CF Bug 82734
-        * (but don't forgeto to add an empty FN= and EF= ,  or webmessenger 
will break. (CF Bug 102371) )
-       else head+="FN=MS%20Serif; EF=; ";
-        */
-
-       // Color support
-       if (msg.foregroundColor().isValid())
+       if ( inkSend )
        {
-               QString colorCode = 
QColor(msg.foregroundColor().blue(),msg.foregroundColor().green(),msg.foregroundColor().red()).name().remove(0,1);
  //colors aren't sent in RGB but in BGR (O.G.)
-               head += "CO=" + colorCode;
+               inkArgs = "N";
+               head =
+                       "MIME-Version: 1.0\r\n"
+                       "Content-Type: image/gif\r\n";
        }
        else
        {
-               head += "CO=0";
-       }
+               // NOTE: Is this safe in an ink draw??
+               // User-Agent is not a official flag, but GAIM has it
+               QString UA;
+               if( config.readEntry("SendClientInfo", true) )
+               {
+                       UA="User-Agent: 
Kopete/"+escape(KGlobal::mainComponent().aboutData()->version())+"\r\n";
+               }
 
-       head += "; CS=0; PF=0";
-       if (msg.plainBody().isRightToLeft())
-               head += "; RL=1";
-       head += "\r\n";
+               head =
+                       "MIME-Version: 1.0\r\n"
+                       "Content-Type: text/plain; charset=UTF-8\r\n"
+                       "User-Agent: 
Kopete/"+escape(KGlobal::mainComponent().aboutData()->version())+"\r\n"
+                       "X-MMS-IM-Format: ";
 
-       QString message= msg.plainBody().replace(  '\n' , "\r\n" );
+               if(msg.font() != QFont() )
+               {
+                       //It's verry strange that if the font name is bigger 
than 31 char, the _server_ close the socket and don't deliver the message.
+                       //  the real question is why ?   my guess is that MS 
patched the server because a bug in their client,  but that's just a guess.
+                       head += "FN=" + escape( msg.font().family().left(31));
+                       head += "; EF=";
+                       if(msg.font().bold())
+                               head += 'B';
+                       if(msg.font().italic())
+                               head += 'I';
+                       if(msg.font().strikeOut())
+                               head += 'S';
+                       if(msg.font().underline())
+                               head += 'U';
+                       head += "; ";
+               }
+               else head+="FN=; EF=; ";
+               /*
+               * I don't know what to set by default, so i decided to set 
nothing.  CF Bug 82734
+               * (but don't forgeto to add an empty FN= and EF= ,  or 
webmessenger will break. (CF Bug 102371) )
+               else head+="FN=MS%20Serif; EF=; ";
+               */
 
+               // Color support
+               if (msg.foregroundColor().isValid())
+               {
+                       QString colorCode = 
QColor(msg.foregroundColor().blue(),msg.foregroundColor().green(),msg.foregroundColor().red()).name().remove(0,1);
  //colors aren't sent in RGB but in BGR (O.G.)
+                       head += "CO=" + colorCode;
+               }
+               else
+               {
+                       head += "CO=0";
+               }
+
+               head += "; CS=0; PF=0";
+               if (msg.plainBody().isRightToLeft())
+                       head += "; RL=1";
+       
+               head += "\r\n";
+               message= msg.plainBody().replace(  '\n' , "\r\n" );
+       }
+       
        //-- Check if the message isn't too big,  TODO: do that at the 
libkopete level.
        int len_H=head.toUtf8().length();       // != head.length()  because i 
need the size in butes and
        int len_M=message.toUtf8().length();    //    some utf8 char may be 
longer than one byte
@@ -778,8 +803,7 @@
                //int futurmessages_size=1664-len_H;
 
                int nb=(int)ceil((float)(len_M)/(float)(futurmessages_size));
-
-               if(KMessageBox::warningContinueCancel(0L /* FIXME: we should 
try to find a parent somewere*/ ,
+               if( inkSend || KMessageBox::warningContinueCancel(0L /* FIXME: 
we should try to find a parent somewere*/ ,
                        i18n("The message you are trying to send is too long; 
it will be split into %1 messages.", nb) ,
                        i18n("Message too big - MSN Plugin" ), 
KStandardGuiItem::cont(), KStandardGuiItem::cancel(), "SendLongMessages" )
                                == KMessageBox::Continue )
@@ -819,7 +843,7 @@
                                {
                                        kDebug(14140) << k_funcinfo <<"The 
message is slit in more than initially estimated" <<endl;
                                }
-                               result=sendCommand( "MSG", "A", true, 
(head+chunk_str+"\r\n"+m).toUtf8() );
+                               result=sendCommand( "MSG", inkArgs, true, 
(head+chunk_str+"\r\n"+m).toUtf8() );
                                chunk++;
                        }
                        while(place < len_M) ;
@@ -828,10 +852,10 @@
                        {
                                kDebug(14140) << k_funcinfo <<"The message is 
plit in less than initially estimated.  Sending empty message to complete" 
<<endl;
                                QString chunk_str="Chunk: 
"+QString::number(chunk);
-                               sendCommand( "MSG", "A", true, 
(head+chunk_str+"\r\n").toUtf8() );
+                               sendCommand( "MSG", inkArgs, true, 
(head+chunk_str+"\r\n").toUtf8() );
                                chunk++;
                        }
-                       return result;
+                       return ( inkSend ? -3 : result );
                }
                return -2;  //the message hasn't been sent.
        }
@@ -844,8 +868,8 @@
                m_keepAlive->start(50*1000);
        }
 
-       
-       return sendCommand( "MSG", "A", true, (head+"\r\n"+message).toUtf8() );
+       int result = sendCommand( "MSG", inkArgs, true, 
(head+"\r\n"+message).toUtf8() );
+       return ( inkSend ? -3 : result );
 }
 
 void MSNSwitchBoardSocket::slotSocketClosed( )
_______________________________________________
kopete-devel mailing list
[email protected]
https://mail.kde.org/mailman/listinfo/kopete-devel

Reply via email to