📄 mmscomposer.cpp
字号:
//qDebug("message part content type %s", part.contentType().toLatin1()); QByteArray data = QByteArray::fromBase64(part.rawEncodedBody()); if( part.contentType().toLower().contains( "text" ) ) { QTextStream stream( data, QIODevice::ReadOnly ); QString t = stream.readAll(); curSlide->textContent()->setText( t ); handledParts.setBit(i); } else if( part.contentType().toLower().contains( "image" ) ) { QPixmap pix; QDataStream stream( &data, QIODevice::ReadOnly ); stream >> pix; curSlide->imageContent()->setImage( pix ); handledParts.setBit(i); } else if( part.contentType().toLower().contains( "audio" ) ) { curSlide->audioContent()->setAudio( data, part.contentLocation() ); handledParts.setBit(i); } } } // attach anything that hasn't been dealt with yet for (uint i = 0 ; i < mail.messagePartCount() ; ++i) { if (!handledParts[i]) { MailMessagePart &part = mail.messagePartAt(i); mail.validateFile(part); // XXX save these parts } }}bool MMSComposer::hasContent() const{ for( uint i = 0 ; i < slideCount() ; ++i ) if( !slide( i )->imageContent()->image().isNull() || !slide( i )->textContent()->text().isEmpty() || !slide( i )->audioContent()->audio().isEmpty()) { return true; } return false;}void MMSComposer::clear(){ while( slideCount() > 1 ) removeSlide( slideCount() - 1 ); if( slideCount() ) { MMSSlide *cur = slide( currentSlide() ); cur->imageContent()->setImage( QPixmap() ); cur->textContent()->setText( QString() ); cur->audioContent()->setAudio( QContent() ); }}MMSComposerInterface::MMSComposerInterface( QObject *parent, const char *name ) : ComposerInterface( parent, name ){ QWidget *par = 0; if( parent && parent->isWidgetType() ) par = (QWidget *)parent; m_composer = new MMSComposer(par); connect( m_composer, SIGNAL(contentChanged()), this, SIGNAL(contentChanged()) );}MMSComposerInterface::~MMSComposerInterface(){ delete m_composer;}/*QCString MMSComposerInterface::id(){ QCString t = QString::number( type() ).toLatin1(); return t + "-Default"; // default mms composer}ComposerInterface::ComposerType MMSComposerInterface::type(){ return MailMessage::MMS;}QString MMSComposerInterface::displayName(){ return tr("MMS");}*/bool MMSComposerInterface::hasContent() const{ return m_composer->hasContent();}void MMSComposerInterface::setMailMessage( Email &mail ){ m_composer->setMailMessage( mail );}void MMSComposerInterface::getContent( MailMessage &mmsMail ) const{ QString doc; QStringList files; // calling this will remove any previous smil messages' data static QString contentDir; if (contentDir.isEmpty()) contentDir = Qtopia::tempDir() + "mms/"; if( QFile::exists( contentDir ) ) { QFile f( contentDir ); f.remove(); } QDir dir( contentDir ); if( !dir.exists() ) dir.mkdir( contentDir ); if( !dir.exists() ) { qWarning() << "Can't create directory for MMS content" << contentDir; return; } QStringList oldFiles = dir.entryList(); for( QStringList::ConstIterator it = oldFiles.begin() ; it != oldFiles.end() ; ++it ) { QFile f( contentDir + *it ); f.remove(); } //clean slate, generate document QString rootLayout, regions, parts, part; static const QString docTemplate = "<smil>\n" " <head>\n" " <meta name=\"title\" content=\"mms\"/>\n" " <meta name=\"author\" content=\"%1\"/>\n" " <layout>\n" " %2\n" " </layout>\n" " </head>\n" " <body>\n" " %3\n" " </body>\n" "</smil>\n" ; // 1.author 2.rootlayout®ions 3.parts static const QString rootLayoutTemplate = "<root-layout width=\"%1\" height=\"%2\"/>\n" ; // 1.width 2.height static const QString regionTemplate = "<region id=\"%1\" width=\"%2\" height=\"%3\" left=\"%4\" top=\"%5\" background-color=\"%6\"/>\n" ; // 1.id 2.width 3.height 4.left 5.top static const QString partTemplate = "<par dur=\"%1\">\n" " %2\n" "</par>\n" ; // 1.duration 2.contentitems static const QString imageTemplate = "<img src=\"%1\" region=\"%2\"/>\n" ; // 1.src 2.region static const QString textTemplate = "<text src=\"%1\" region=\"%2\"/>\n" ; // 1.src 2.region static const QString audioTemplate = "<audio src=\"%1\"/>\n" ; // 1.src /* if the composer only contains one piece of content either an image or some text, then we dont need to generate smil output */ int contentCount = 0; MMSSlideImage *imageContent = 0; MMSSlideText *textContent = 0; MMSSlideAudio *audioContent = 0; for( uint s = 0 ; s < m_composer->slideCount() ; ++s ) { MMSSlide *curSlide = m_composer->slide( s ); if( !curSlide->imageContent()->image().isNull() ) { imageContent = curSlide->imageContent(); ++contentCount; } if( !curSlide->textContent()->text().isNull() ) { textContent = curSlide->textContent(); ++contentCount; } if( !curSlide->audioContent()->audio().isEmpty() ) { audioContent = curSlide->audioContent(); ++contentCount; } if( contentCount > 1 ) break; } if( contentCount == 1 ) { if( imageContent ) { QString f = QString( contentDir ) + "mmsimage.jpg"; imageContent->image().save( f, "JPEG" ); files.append( f ); } else if( textContent ) { doc = textContent->text(); mmsMail.setPlainTextBody(doc); } else if (audioContent) { QString fn = QString( contentDir ) + "mmsaudio.amr"; QFile f(fn); if (f.open(QIODevice::WriteOnly)) { f.write(audioContent->audio().data(), audioContent->audio().size()); files.append( fn ); } } } else if( contentCount > 1 ) { rootLayout = rootLayoutTemplate.arg( m_composer->contentsRect().width() ) .arg( m_composer->contentsRect().height() ); QRect largestText, largestImage; for( int s = 0 ; s < (int)m_composer->slideCount() ; ++s ) { MMSSlide *curSlide = m_composer->slide( s ); imageContent = curSlide->imageContent(); textContent = curSlide->textContent(); audioContent = curSlide->audioContent(); part = QString(); if( !imageContent->image().isNull() ) { QRect cr = imageContent->contentsRect(); if( cr.width() > largestImage.width() ) largestImage.setWidth( cr.width() ); if( cr.height() > largestImage.height() ) largestImage.setHeight( cr.height() ); QString imgFileName = "image" + QString::number( s ) + ".jpg"; if( !imageContent->image().save( contentDir + imgFileName, "JPEG" ) ) { qWarning() << "Cannot save image" << (contentDir+imgFileName); return; } files.append( contentDir + imgFileName ); part += imageTemplate.arg( imgFileName ) .arg( "image" ); } if( !textContent->text().isNull() ) { QRect cr = textContent->contentsRect(); if( cr.height() > largestText.height() ) { largestText.setHeight( cr.height() ); } QString textFileName = "text" + QString::number( s ) + ".txt"; QFile f( contentDir + textFileName ); if( !f.open( QIODevice::WriteOnly ) ) { qWarning() << "Cannot open text file for writing:" << textFileName; return; } QTextStream stream( &f ); stream << textContent->text(); files.append( contentDir + textFileName ); part += textTemplate.arg( textFileName ) .arg( "text" ); } if( !audioContent->audio().isEmpty() ) { QString fn = "mmsaudio" + QString::number( s ) + ".amr"; QFile f(contentDir + fn); if (f.open(QIODevice::WriteOnly)) { f.write(audioContent->audio().data(), audioContent->audio().size()); files.append( contentDir + fn ); } part += audioTemplate.arg(fn); } if( !part.isEmpty() ) { part = partTemplate.arg( QString::number( curSlide->duration() ) + "ms" ) .arg( part ) ; parts += part; } } QRect imageRect; imageRect.setX( 0 ); imageRect.setY( 0 ); imageRect.setWidth( m_composer->contentsRect().width() ); imageRect.setHeight( m_composer->contentsRect().height() - largestText.height() ); largestText.setX( 0 ); int h = largestText.height(); largestText.setY( imageRect.height() ); largestText.setHeight( h ); largestText.setWidth(m_composer->contentsRect().width()); if( imageRect.width() > 0 && imageRect.height() > 0 ) { regions += regionTemplate.arg( "image" ) .arg( imageRect.width() ) .arg( imageRect.height() ) .arg( imageRect.x() ) .arg( imageRect.y() ) .arg( m_composer->backgroundColor().name().toUpper() ); ; } if( largestText.width() > 0 && largestText.height() > 0 ) { regions += regionTemplate.arg( "text" ) .arg( largestText.width() ) .arg( largestText.height() ) .arg( largestText.x() ) .arg( largestText.y() ) .arg( m_composer->backgroundColor().name().toUpper() ); ; } doc = docTemplate.arg( Qtopia::ownerName() ) .arg( rootLayout + regions ) .arg( parts ) ; // add the smil document to the message mmsMail.setMultipartRelated(true); MailMessagePart docPart; docPart.setContentType( "application/smil" ); docPart.setContentID( "<presentation-part>" ); docPart.setEncodedBody( doc, EightBit ); mmsMail.addMessagePart( docPart ); } mmsMail.setType( MailMessage::MMS ); // add binary data as mail attachments for( QStringList::ConstIterator it = files.begin() ; it != files.end() ; ++it ) { MailMessagePart part; QFileInfo fi( *it ); part.setName( fi.baseName() ); part.setContentLocation( fi.fileName() ); part.setFilename( fi.absoluteFilePath() ); part.setStoredFilename( fi.absoluteFilePath() ); part.setLinkFile( fi.absoluteFilePath() ); QMimeType mt( fi.absoluteFilePath() ); part.setContentType( mt.id() ); QFile f( fi.absoluteFilePath() ); if( !f.open( QIODevice::ReadOnly ) ) { qWarning() << "Could not open MMS attachment for reading" << fi.absoluteFilePath(); continue; } part.setEncodedBody( MailMessage::encodeBase64( f.readAll() ), Base64 ); mmsMail.addMessagePart( part ); }}void MMSComposerInterface::attach( const QContent &lnk ){ if (!m_composer->slideCount()) m_composer->addSlide(); MMSSlide *curSlide = m_composer->slide( m_composer->slideCount()-1 ); if (lnk.type().startsWith("image/")) { QPixmap pm(lnk.file()); if (!pm.isNull()) { if (!curSlide->imageContent()->image().isNull()) { // If there is already an image in the last slide, add a new one m_composer->addSlide(); curSlide = m_composer->slide( m_composer->slideCount()-1 ); } curSlide->imageContent()->setImage(pm); } } else if (lnk.type() == "text/plain") { QFile file(lnk.file()); if (file.open(QIODevice::ReadOnly)) { QTextStream ts(&file); if (!curSlide->textContent()->text().isEmpty()) { // If there is already text in the last slide, add a new one m_composer->addSlide(); curSlide = m_composer->slide( m_composer->slideCount()-1 ); } curSlide->textContent()->setText(ts.readAll()); } } else if (lnk.type().startsWith("audio/")) { if (!curSlide->audioContent()->audio().isEmpty()) { // If there is already audio in the last slide, add a new one m_composer->addSlide(); curSlide = m_composer->slide( m_composer->slideCount()-1 ); } curSlide->audioContent()->setAudio(lnk); } else { // XXX deal with other attachments }}void MMSComposerInterface::attach( const QString &fileName ){ attach(QContent(fileName));}void MMSComposerInterface::clear(){ m_composer->clear();}QWidget *MMSComposerInterface::widget() const{ return m_composer;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -