Exemple #1
0
/** @short Take the initial text and mark it as a quotation */
QStringList quoteText(QStringList inputLines)
{
    QStringList quote;
    for (QStringList::iterator line = inputLines.begin(); line != inputLines.end(); ++line) {
        if (UiUtils::signatureSeparator().match(*line).hasMatch()) {
            // This is the signature separator, we should not include anything below that in the quote
            break;
        }
        // rewrap - we need to keep the quotes at < 79 chars, yet the grow with every level
        if (line->length() < 79-2) {
            // this line is short enough, prepend quote mark and continue
            if (line->isEmpty() || line->at(0) == QLatin1Char('>'))
                line->prepend(QLatin1Char('>'));
            else
                line->prepend(QLatin1String("> "));
            quote << *line;
            continue;
        }
        // long line -> needs to be wrapped
        // 1st, detect the quote depth and eventually stript the quotes from the line
        int quoteLevel = 0;
        int contentStart = 0;
        if (line->at(0) == QLatin1Char('>')) {
            quoteLevel = 1;
            while (quoteLevel < line->length() && line->at(quoteLevel) == QLatin1Char('>'))
                ++quoteLevel;
            contentStart = quoteLevel;
            if (quoteLevel < line->length() && line->at(quoteLevel) == QLatin1Char(' '))
                ++contentStart;
        }

        // 2nd, build a quote string
        QString quotemarks;
        for (int i = 0; i < quoteLevel; ++i)
            quotemarks += QLatin1Char('>');
        quotemarks += QLatin1String("> ");

        // 3rd, wrap the line, prepend the quotemarks to each line and add it to the quote text
        int space(contentStart), lastSpace(contentStart), pos(contentStart), length(0);
        while (pos < line->length()) {
            if (line->at(pos) == QLatin1Char(' '))
                space = pos+1;
            ++length;
            if (length > 65-quotemarks.length() && space != lastSpace) {
                // wrap
                quote << quotemarks + line->mid(lastSpace, space - lastSpace);
                lastSpace = space;
                length = pos - space;
            }
            ++pos;
        }
        quote << quotemarks + line->mid(lastSpace);
    }
    return quote;
}
Exemple #2
0
void GoogleSession::authResult(bool errorFlag)
{
    if (errorFlag)
    {
      qDebug() << "Auth http error" << http->errorString();
      setState(Invalid);
      emit error(AuthenticationFailed, http->errorString());
    }
    else
    {
      QString resp = http->readAll(); 
      //qDebug() << resp;
      QStringList keys = resp.split("\n");
      QHash<QString, QString> keyMap;
      for (QStringList::iterator it = keys.begin(); it!=keys.end(); it++)
      {
        int sep = it->indexOf('=');
        QString key = it->left(sep);
        QString value = it->right(it->length()-sep-1);
        keyMap[key] = value;
        //qDebug() << key << value;
      }
      if (http->lastResponse().statusCode()==200) // OK
      {
        if (keyMap.contains("Auth"))
        {
          authKey = keyMap["Auth"];
          qDebug() << "Authenticated" << authKey;
          setState(Authenticated);
          emit authenticated();
        }
        else
        {
          setState(Invalid);
          emit error(AuthenticationFailed, "No Auth key");
        }
      }
      else
      {
        qDebug() << "ERROR Response header:" << http->lastResponse().statusCode() << http->lastResponse().reasonPhrase();
        qDebug() << "ERROR reason" << keyMap["Error"];
        setState(Invalid);
        emit error(AuthenticationFailed, keyMap["Error"]);
      }
    }
}
Exemple #3
0
QString MessageView::quoteText() const
{
    if (const AbstractPartWidget *w = dynamic_cast<const AbstractPartWidget *>(viewer)) {
        QStringList quote;
        QStringList lines = w->quoteMe().split('\n');
        for (QStringList::iterator line = lines.begin(); line != lines.end(); ++line) {
            if (Composer::Util::signatureSeparator().exactMatch(*line)) {
                // This is the signature separator, we should not include anything below that in the quote
                break;
            }
            // rewrap - we need to keep the quotes at < 79 chars, yet the grow with every level
            if (line->length() < 79-2) {
                // this line is short enough, prepend quote mark and continue
                if (line->isEmpty() || line->at(0) == '>')
                    line->prepend(">");
                else
                    line->prepend("> ");
                quote << *line;
                continue;
            }
            // long line -> needs to be wrapped
            // 1st, detect the quote depth and eventually stript the quotes from the line
            int quoteLevel = 0;
            int contentStart = 0;
            if (line->at(0) == '>') {
                quoteLevel = 1;
                while (quoteLevel < line->length() && line->at(quoteLevel) == '>')
                    ++quoteLevel;
                contentStart = quoteLevel;
                if (quoteLevel < line->length() && line->at(quoteLevel) == ' ')
                    ++contentStart;
            }

            // 2nd, build a qute string
            QString quotemarks;
            for (int i = 0; i < quoteLevel; ++i)
                quotemarks += ">";
            quotemarks += "> ";

            // 3rd, wrap the line, prepend the quotemarks to each line and add it to the quote text
            int space(contentStart), lastSpace(contentStart), pos(contentStart), length(0);
            while (pos < line->length()) {
                if (line->at(pos) == ' ')
                    space = pos+1;
                ++length;
                if (length > 65-quotemarks.length() && space != lastSpace) {
                    // wrap
                    quote << quotemarks + line->mid(lastSpace, space - lastSpace);
                    lastSpace = space;
                    length = pos - space;
                }
                ++pos;
            }
            quote << quotemarks + line->mid(lastSpace);
        }
        const Imap::Message::Envelope &e = message.data(Imap::Mailbox::RoleMessageEnvelope).value<Imap::Message::Envelope>();
        QString sender;
        if (!e.from.isEmpty())
            sender = e.from[0].prettyName(Imap::Message::MailAddress::FORMAT_JUST_NAME);
        if (e.from.isEmpty())
            sender = tr("you");

        // One extra newline at the end of the quoted text to separate the response
        quote << QString();

        return tr("On %1, %2 wrote:\n").arg(e.date.toLocalTime().toString(Qt::SystemLocaleLongDate)).arg(sender) + quote.join("\n");
    }
    return QString();
}
ConversionStatus GettextExportPlugin::save(QIODevice* device,
                                           const GettextStorage* catalog)
{
    QTextStream stream(device);

    //if ( m_wrapWidth == -1 ) m_wrapWidth=80;

#if 0
//legacy
    if (useOldEncoding && catalog->fileCodec())
    {
        stream.setCodec(catalog->fileCodec());
    }
    else

    {
        /*         switch(_saveSettings.encoding)
                 {
                    case ProjectSettingsBase::UTF8:
                       stream.setCodec(QTextCodec::codecForName("utf-8"));
                       break;
                    case ProjectSettingsBase::UTF16:
                       stream.setCodec(QTextCodec::codecForName("utf-16"));
                       break;
                    default:
        	       stream.setCodec(QTextCodec::codecForLocale());
                       break;
                 }*/
#endif
    //NOTE i had a look and even ja team uses utf-8 now
    stream.setCodec(QTextCodec::codecForName("utf-8"));


    // only save header if it is not empty
    const QString& headerComment( catalog->m_header.comment() );
    // ### why is this useful to have a header with an empty msgstr?
    if ( !headerComment.isEmpty() || !catalog->m_header.msgstrPlural().isEmpty() )
    {
        // write header
        writeComment( stream, headerComment );

        const QString& headerMsgid (catalog->m_header.msgid());

        // Gettext PO files should have an empty msgid as header
        if ( !headerMsgid.isEmpty() )
        {
            // ### perhaps it is grave enough for a user message
            kWarning() << "Non-empty msgid for the header, assuming empty msgid!" << endl << headerMsgid << "---";
        }

        // ### FIXME: if it is the header, then the msgid should be empty! (Even if KBabel has made something out of a non-header first entry!)
        stream << "msgid \"\"\n";

        writeKeyword( stream, "msgstr", catalog->m_header.msgstr(), false );
    }


    const QVector<CatalogItem>& catalogEntries=catalog->m_entries;
    int limit=catalog->numberOfEntries();
    QStringList list;
    for (int counter = 0; counter < limit; counter++)
    {
        stream << '\n';

        // write entry
        writeComment( stream, catalogEntries.at(counter).comment() );

        const QString& msgctxt = catalogEntries.at(counter).msgctxt();
        if (! msgctxt.isEmpty() )
            writeKeyword( stream, "msgctxt", msgctxt );

        writeKeyword( stream, "msgid", catalogEntries.at(counter).msgid(), true, catalogEntries.at(counter).prependEmptyForMsgid() );
        if ( catalogEntries.at(counter).isPlural() )
            writeKeyword( stream, "msgid_plural", catalogEntries.at(counter).msgid(1), true, catalogEntries.at(counter).prependEmptyForMsgid() );

        if (!catalogEntries.at(counter).isPlural())
            writeKeyword( stream, "msgstr", catalogEntries.at(counter).msgstr(), true, catalogEntries.at(counter).prependEmptyForMsgstr() );
        else
        {
            kDebug() << "Saving gettext plural form";
            //TODO check len of the actual stringlist??
            const int forms = catalog->numberOfPluralForms();
            for ( int i = 0; i < forms; ++i )
            {
                QString keyword = "msgstr[" % QString::number( i ) % ']';
                writeKeyword( stream, keyword, catalogEntries.at(counter).msgstr(i), true, catalogEntries.at(counter).prependEmptyForMsgstr() );
            }
        }
    }

#if 0
//legacy
    if ( _saveSettings.saveObsolete )
#endif
    {
        QList<QString>::const_iterator oit;
        const QStringList& _obsolete=catalog->m_catalogExtraData;
        oit=_obsolete.constBegin();
        if (oit!=_obsolete.constEnd())
        {
            stream << "\n" << (*oit);
            while((++oit)!=_obsolete.constEnd())
                stream << "\n\n" << (*oit);
        }
    }

    int i=m_trailingNewLines+1;
    while (--i>=0)
        stream << '\n';

    return OK;
}

void GettextExportPlugin::writeComment( QTextStream& stream, const QString& comment ) const
{
    if( !comment.isEmpty() )
    {
        // We must check that each comment line really starts with a #, to avoid syntax errors
        int pos = 0;
        for(;;)
        {
            const int newpos = comment.indexOf( '\n', pos, Qt::CaseInsensitive );
            if ( newpos == pos )
            {
                ++pos;
                stream << '\n';
                continue;
            }
            const QString& span ((newpos==-1 ) ? comment.mid(pos) : comment.mid(pos, newpos-pos) );

            const int len = span.length();
            QString spaces; // Stored leading spaces
            for ( int i = 0 ; i < len ; ++i )
            {
                const QChar& ch = span[ i ];
                if ( ch == '#' )
                {
                    stream << spaces << span.mid( i );
                    break;
                }
                else if ( ch == ' ' || ch == '\t' )
                {
                    // We have a leading white space character, so store it temporary
                    spaces += ch;
                }
                else
                {
                    // Not leading white space and not a # character. so consider that the # character was missing at first position.
                    stream << "# " << spaces << span.mid( i );
                    break;
                }
            }
            stream << '\n';

            if ( newpos == -1 )
                break;
            else
                pos = newpos + 1;
        }
    }
}

void GettextExportPlugin::writeKeyword( QTextStream& stream, const QString& keyword, QString text, bool containsHtml, bool startedWithEmptyLine ) const
{
    if ( text.isEmpty() )
    {
        // Whatever the wrapping mode, an empty line is an empty line
        stream << keyword << " \"\"\n";
        return;
    }

    //TODO remove this for KDE 4.4
    //NOTE not?
    int pos=0;
    while ((pos=text.indexOf("\\\"",pos))!=-1)
    {
        if (pos==0 || text.at(pos-1)!='\\')
            text.replace(pos,2,'"');
        else
            pos++;
    }
    text.replace('"',"\\\"");
#if 0
    if ( m_wrapWidth == -1 )
    {
        // Traditional KBabel wrapping
        QStringList list = text.split( '\n', QString::SkipEmptyParts );

        if ( text.startsWith( '\n' ) )
            list.prepend( QString() );

        if(list.isEmpty())
            list.append( QString() );

        if( list.count() > 1 )
            list.prepend( QString() );

        stream << keyword << ' ';

        QStringList::const_iterator it;
        for( it = list.constBegin(); it != list.constEnd(); ++it )
            stream << '\"' << (*it) << "\"\n";
        return;
    }
#endif

    if ( m_wrapWidth == 0 ) // Unknown special wrapping, so assume "no wrap" instead
    {
        // No wrapping (like Gettext's --no.wrap or -w0 )
        // we need to remove the \n characters, as they are extra characters
        QString realText( text );
        realText.remove( '\n' );
        stream << keyword << " \"" << realText << "\"\n";
        return;
    }
    else if ( m_wrapWidth < 0 )
    {
        // No change in wrapping
        QStringList list = text.split( '\n');
        if (list.count()>1 || startedWithEmptyLine /* || keyword.length()+3+text.length()>=80*/)
            list.prepend(QString());

        stream << keyword << " ";
        QStringList::const_iterator it;
        for( it = list.constBegin(); it != list.constEnd(); ++it )
            stream << "\"" << (*it) << "\"\n";

        return;
    }

    // lazy wrapping
    QStringList list = text.split( '\n', QString::SkipEmptyParts );

    if ( text.startsWith( '\n' ) )
        list.prepend( QString() );

    if(list.isEmpty())
        list.append( QString() );

    static QRegExp breakStopReForHtml("[ >.%]", Qt::CaseSensitive, QRegExp::Wildcard);
    QRegExp breakStopRe=containsHtml?breakStopReForHtml:QRegExp("[ .%]", Qt::CaseSensitive, QRegExp::Wildcard);

    int max=m_wrapWidth-2;
    bool prependedEmptyLine=false;
    QStringList::iterator itm;
    for( itm = list.begin(); itm != list.end(); ++itm )
    {
        if (list.count()==1 && keyword.length()+1+itm->length()>=max)
        {
            prependedEmptyLine=true;
            itm=list.insert(itm,QString());
        }

        if (itm->length()>max)
        {
            int pos = itm->lastIndexOf(breakStopRe,max-1);
            if (pos>0)
            {
                int pos2 = itm->indexOf('<',pos);
                if (pos2>0&&pos2<max-1)
                    pos=itm->indexOf('<',pos);
                ++pos;
            }
            else
                pos=max;
            //itm=list.insert(itm,itm->left(pos));
            QString t=*itm;
            itm=list.insert(itm,t);
            itm++;
            if (itm != list.end())
            {
                (*itm)=itm->remove(0,pos);
                itm--;
                if (itm != list.end())
                    itm->truncate(pos);
            }
        }
    }

    if( !prependedEmptyLine && list.count() > 1 )
        list.prepend( QString() );

    stream << keyword << " ";

    QStringList::const_iterator it;
    for( it = list.constBegin(); it != list.constEnd(); ++it )
        stream << "\"" << (*it) << "\"\n";
}