PsMainWindow::PsMainWindow(QWidget *parent) : QMainWindow(parent), posInited(false), trayIcon(0), trayIconMenu(0), icon256(qsl(":/gui/art/icon256.png")), iconbig256(qsl(":/gui/art/iconbig256.png")), wndIcon(QPixmap::fromImage(iconbig256, Qt::ColorOnly)), psLogout(0), psUndo(0), psRedo(0), psCut(0), psCopy(0), psPaste(0), psDelete(0), psSelectAll(0), psContacts(0), psAddContact(0), psNewGroup(0), psNewChannel(0), psShowTelegram(0) { QImage tray(qsl(":/gui/art/osxtray.png")); trayImg = tray.copy(0, cRetina() ? 0 : tray.width() / 2, tray.width() / (cRetina() ? 2 : 4), tray.width() / (cRetina() ? 2 : 4)); trayImgSel = tray.copy(tray.width() / (cRetina() ? 2 : 4), cRetina() ? 0 : tray.width() / 2, tray.width() / (cRetina() ? 2 : 4), tray.width() / (cRetina() ? 2 : 4)); }
void PsMainWindow::psUpdateCounter() { int32 counter = App::histories().unreadFull - (cIncludeMuted() ? 0 : App::histories().unreadMuted); setWindowTitle((counter > 0) ? qsl("Telegram (%1)").arg(counter) : qsl("Telegram")); setWindowIcon(wndIcon); QString cnt = (counter < 1000) ? QString("%1").arg(counter) : QString("..%1").arg(counter % 100, 2, 10, QChar('0')); _private.setWindowBadge(counter ? cnt : QString()); if (trayIcon) { bool muted = cIncludeMuted() ? (App::histories().unreadMuted >= counter) : false; bool dm = objc_darkMode(); style::color bg = muted ? st::counterMuteBG : st::counterBG; QIcon icon; QImage img(psTrayIcon(dm)), imgsel(psTrayIcon(true)); img.detach(); imgsel.detach(); int32 size = cRetina() ? 44 : 22; _placeCounter(img, size, counter, bg, (dm && muted) ? st::counterMacInvColor : st::counterColor); _placeCounter(imgsel, size, counter, st::white, st::counterMacInvColor); icon.addPixmap(QPixmap::fromImage(img, Qt::ColorOnly)); icon.addPixmap(QPixmap::fromImage(imgsel, Qt::ColorOnly), QIcon::Selected); trayIcon->setIcon(icon); } }
void TitleWidget::updateCounter() { if (cWideMode() || !MTP::authedId()) return; int32 counter = App::histories().unreadFull - (cIncludeMuted() ? 0 : App::histories().unreadMuted); bool muted = cIncludeMuted() ? (App::histories().unreadMuted >= counter) : false; style::color bg = muted ? st::counterMuteBG : st::counterBG; if (counter > 0) { int32 size = cRetina() ? -32 : -16; switch (cScale()) { case dbisOneAndQuarter: size = -20; break; case dbisOneAndHalf: size = -24; break; case dbisTwo: size = -32; break; } _counter = QPixmap::fromImage(App::wnd()->iconWithCounter(size, counter, bg, false), Qt::ColorOnly); _counter.setDevicePixelRatio(cRetinaFactor()); update(QRect(st::titleIconPos, st::titleIconImg.pxSize())); } else { if (!_counter.isNull()) { _counter = QPixmap(); update(QRect(st::titleIconPos, st::titleIconImg.pxSize())); } } }
void TitleWidget::updateCounter() { if (!Adaptive::OneColumn() || !MTP::authedId()) return; int32 counter = App::histories().unreadBadge(); bool muted = App::histories().unreadOnlyMuted(); style::color bg = muted ? st::counterMuteBG : st::counterBG; if (counter > 0) { int32 size = cRetina() ? -32 : -16; switch (cScale()) { case dbisOneAndQuarter: size = -20; break; case dbisOneAndHalf: size = -24; break; case dbisTwo: size = -32; break; } _counter = App::pixmapFromImageInPlace(App::wnd()->iconWithCounter(size, counter, bg, false)); _counter.setDevicePixelRatio(cRetinaFactor()); update(QRect(st::titleCounterPosition, _counter.size() / cIntRetinaFactor())); } else { if (!_counter.isNull()) { update(QRect(st::titleCounterPosition, _counter.size() / cIntRetinaFactor())); _counter = QPixmap(); } } }
QPixmap myGrab(QWidget *target, const QRect &rect) { if (!cRetina()) return target->grab(rect); myEnsureResized(target); qreal dpr = App::app()->devicePixelRatio(); QPixmap result(rect.size() * dpr); result.setDevicePixelRatio(dpr); result.fill(Qt::transparent); target->render(&result, QPoint(), QRegion(rect), QWidget::DrawWindowBackground | QWidget::DrawChildren | QWidget::IgnoreMask); return result; }
const QPixmap &Image::pixBlurred(int32 w, int32 h) const { restore(); checkload(); if (w <= 0 || !width() || !height()) { w = width() * cIntRetinaFactor(); } else if (cRetina()) { w *= cIntRetinaFactor(); h *= cIntRetinaFactor(); } uint64 k = 0x8000000000000000L | (uint64(w) << 32) | uint64(h); Sizes::const_iterator i = _sizesCache.constFind(k); if (i == _sizesCache.cend()) { QPixmap p(pixBlurredNoCache(w, h)); if (cRetina()) p.setDevicePixelRatio(cRetinaFactor()); i = _sizesCache.insert(k, p); if (!p.isNull()) { globalAquiredSize += int64(p.width()) * p.height() * 4; } } return i.value(); }
QPixmap myGrab(QWidget *target, const QRect &rect) { if (!cRetina()) return target->grab(rect); if (target->testAttribute(Qt::WA_PendingResizeEvent) || !target->testAttribute(Qt::WA_WState_Created)) { _sendResizeEvents(target); } qreal dpr = App::app()->devicePixelRatio(); QPixmap result(rect.size() * dpr); result.setDevicePixelRatio(dpr); result.fill(Qt::transparent); target->render(&result, QPoint(), QRegion(rect), QWidget::DrawWindowBackground | QWidget::DrawChildren | QWidget::IgnoreMask); return result; }
void LocationManager::getData(LocationData *data) { if (!manager) { DEBUG_LOG(("App Error: getting image link data without manager init!")); return failed(data); } int32 w = st::locationSize.width(), h = st::locationSize.height(); int32 zoom = 13, scale = 1; if (cScale() == dbisTwo || cRetina()) { scale = 2; } else { w = convertScale(w); h = convertScale(h); } QString coords = qsl("%1,%2").arg(data->coords.lat).arg(data->coords.lon); QString url = qsl("https://maps.googleapis.com/maps/api/staticmap?center=") + coords + qsl("&zoom=%1&size=%2x%3&maptype=roadmap&scale=%4&markers=color:red|size:big|").arg(zoom).arg(w).arg(h).arg(scale) + coords + qsl("&sensor=false"); QNetworkReply *reply = manager->get(QNetworkRequest(QUrl(url))); imageLoadings[reply] = data; }
void NotifyWindow::updateNotifyDisplay() { if (!item) return; int32 w = st::notifyWidth, h = st::notifyHeight; QImage img(w * cIntRetinaFactor(), h * cIntRetinaFactor(), QImage::Format_ARGB32_Premultiplied); if (cRetina()) img.setDevicePixelRatio(cRetinaFactor()); img.fill(st::notifyBG->c); { QPainter p(&img); p.fillRect(0, 0, w - st::notifyBorderWidth, st::notifyBorderWidth, st::notifyBorder->b); p.fillRect(w - st::notifyBorderWidth, 0, st::notifyBorderWidth, h - st::notifyBorderWidth, st::notifyBorder->b); p.fillRect(st::notifyBorderWidth, h - st::notifyBorderWidth, w - st::notifyBorderWidth, st::notifyBorderWidth, st::notifyBorder->b); p.fillRect(0, st::notifyBorderWidth, st::notifyBorderWidth, h - st::notifyBorderWidth, st::notifyBorder->b); if (cNotifyView() <= dbinvShowName) { if (history->peer->photo->loaded()) { p.drawPixmap(st::notifyPhotoPos.x(), st::notifyPhotoPos.y(), history->peer->photo->pix(st::notifyPhotoSize)); } else { MTP::clearLoaderPriorities(); peerPhoto = history->peer->photo; peerPhoto->load(true, true); } } else { static QPixmap icon = QPixmap::fromImage(App::wnd()->iconLarge().scaled(st::notifyPhotoSize, st::notifyPhotoSize, Qt::IgnoreAspectRatio, Qt::SmoothTransformation)); p.drawPixmap(st::notifyPhotoPos.x(), st::notifyPhotoPos.y(), icon); } int32 itemWidth = w - st::notifyPhotoPos.x() - st::notifyPhotoSize - st::notifyTextLeft - st::notifyClosePos.x() - st::notifyClose.width; QRect rectForName(st::notifyPhotoPos.x() + st::notifyPhotoSize + st::notifyTextLeft, st::notifyTextTop, itemWidth, st::msgNameFont->height); if (cNotifyView() <= dbinvShowName) { if (history->peer->chat) { p.drawPixmap(QPoint(rectForName.left() + st::dlgChatImgLeft, rectForName.top() + st::dlgChatImgTop), App::sprite(), st::dlgChatImg); rectForName.setLeft(rectForName.left() + st::dlgChatImgSkip); } } QDateTime now(QDateTime::currentDateTime()), lastTime(item->date); QDate nowDate(now.date()), lastDate(lastTime.date()); QString dt = lastTime.toString(qsl("hh:mm")); int32 dtWidth = st::dlgHistFont->m.width(dt); rectForName.setWidth(rectForName.width() - dtWidth - st::dlgDateSkip); p.setFont(st::dlgDateFont->f); p.setPen(st::dlgDateColor->p); p.drawText(rectForName.left() + rectForName.width() + st::dlgDateSkip, rectForName.top() + st::dlgHistFont->ascent, dt); if (cNotifyView() <= dbinvShowPreview) { const HistoryItem *textCachedFor = 0; Text itemTextCache(itemWidth); bool active = false; item->drawInDialog(p, QRect(st::notifyPhotoPos.x() + st::notifyPhotoSize + st::notifyTextLeft, st::notifyItemTop + st::msgNameFont->height, itemWidth, 2 * st::dlgFont->height), active, textCachedFor, itemTextCache); } else { static QString notifyText = st::dlgHistFont->m.elidedText(lang(lng_notification_preview), Qt::ElideRight, itemWidth); p.setPen(st::dlgSystemColor->p); p.drawText(st::notifyPhotoPos.x() + st::notifyPhotoSize + st::notifyTextLeft, st::notifyItemTop + st::msgNameFont->height + st::dlgHistFont->ascent, notifyText); } p.setPen(st::dlgNameColor->p); if (cNotifyView() <= dbinvShowName) { history->nameText.drawElided(p, rectForName.left(), rectForName.top(), rectForName.width()); } else { p.setFont(st::msgNameFont->f); static QString notifyTitle = st::msgNameFont->m.elidedText(lang(lng_notification_title), Qt::ElideRight, rectForName.width()); p.drawText(rectForName.left(), rectForName.top() + st::msgNameFont->ascent, notifyTitle); } } pm = QPixmap::fromImage(img); update(); }
void SettingsInner::showAll() { // profile if (_self) { if (App::app()->isPhotoUpdating(_self->id)) { _cancelPhoto.show(); _uploadPhoto.hide(); } else { _cancelPhoto.hide(); _uploadPhoto.show(); } } else { _uploadPhoto.hide(); _cancelPhoto.hide(); } // notifications if (_self) { _desktopNotify.show(); _soundNotify.show(); } else { _desktopNotify.hide(); _soundNotify.hide(); } // general _autoUpdate.show(); setUpdatingState(_updatingState, true); if (cPlatform() == dbipWindows) { _workmodeTray.show(); _workmodeWindow.show(); _autoStart.show(); _startMinimized.show(); } else { _workmodeTray.hide(); _workmodeWindow.hide(); _autoStart.hide(); _startMinimized.hide(); } if (cRetina()) { _dpiSlider.hide(); _dpiAutoScale.hide(); } else { _dpiSlider.show(); _dpiAutoScale.show(); } // chat options if (_self) { _replaceEmojis.show(); if (cReplaceEmojis()) { _viewEmojis.show(); } else { _viewEmojis.hide(); } _enterSend.show(); _ctrlEnterSend.show(); _catsAndDogs.show(); _scrollNotActive.show(); _dontAskDownloadPath.show(); if (cAskDownloadPath()) { _downloadPathEdit.hide(); _downloadPathClear.hide(); } else { _downloadPathEdit.show(); if (cDownloadPath().isEmpty() && _tempDirClearState == TempDirExists) { // dir exists, not clearing right now _downloadPathClear.show(); } else { _downloadPathClear.hide(); } } } else { _replaceEmojis.hide(); _viewEmojis.hide(); _enterSend.hide(); _ctrlEnterSend.hide(); _catsAndDogs.hide(); _scrollNotActive.hide(); _dontAskDownloadPath.hide(); _downloadPathEdit.hide(); _downloadPathClear.hide(); } // advanced if (_self) { if (_resetDone) { _resetSessions.hide(); } else { _resetSessions.show(); } _logOut.show(); } else { _resetSessions.hide(); _logOut.hide(); } }
void SettingsInner::resizeEvent(QResizeEvent *e) { _left = (width() - st::setWidth) / 2; int32 top = 0; if (_self) { // profile top += st::setTop; top += st::setPhotoSize; _uploadPhoto.move(_left + st::setWidth - _uploadPhoto.width(), top - _uploadPhoto.height()); _cancelPhoto.move(_left + st::setWidth - _cancelPhoto.width(), top - _uploadPhoto.height() + st::btnSetUpload.textTop + st::btnSetUpload.font->ascent - st::linkFont->ascent); // notifications top += st::setHeaderSkip; _desktopNotify.move(_left, top); top += _desktopNotify.height() + st::setLittleSkip; _soundNotify.move(_left, top); top += _soundNotify.height(); } // general top += st::setHeaderSkip; _autoUpdate.move(_left, top); _checkNow.move(_left + st::setWidth - _checkNow.width(), top + st::cbDefFlat.textTop); top += _autoUpdate.height(); _restartNow.move(_left + st::setWidth - _restartNow.width(), top + st::setVersionTop); top += st::setVersionHeight; if (cPlatform() == dbipWindows) { _workmodeTray.move(_left, top); top += _workmodeTray.height() + st::setLittleSkip; _workmodeWindow.move(_left, top); top += _workmodeWindow.height() + st::setSectionSkip; _autoStart.move(_left, top); top += _autoStart.height() + st::setLittleSkip; _startMinimized.move(_left, top); top += _startMinimized.height(); } if (!cRetina()) { top += st::setHeaderSkip; _dpiAutoScale.move(_left, top); top += _dpiAutoScale.height() + st::setLittleSkip; _dpiSlider.move(_left, top); top += _dpiSlider.height() + st::dpiFont4->height; } // chat options if (_self) { top += st::setHeaderSkip; _viewEmojis.move(_left + st::setWidth - _viewEmojis.width(), top + st::cbDefFlat.textTop); _replaceEmojis.move(_left, top); top += _replaceEmojis.height() + st::setSectionSkip; _enterSend.move(_left, top); top += _enterSend.height() + st::setLittleSkip; _ctrlEnterSend.move(_left, top); top += _ctrlEnterSend.height() + st::setSectionSkip; _dontAskDownloadPath.move(_left, top); top += _dontAskDownloadPath.height(); if (!cAskDownloadPath()) { top += st::setLittleSkip; _downloadPathEdit.move(_left + st::setVersionLeft + _downloadPathWidth, top); if (cDownloadPath().isEmpty()) { _downloadPathClear.move(_left + st::setWidth - _downloadPathClear.width(), top); } top += _downloadPathEdit.height(); } top += st::setSectionSkip; _catsAndDogs.move(_left, top); top += _catsAndDogs.height() + st::setLittleSkip; _scrollNotActive.move(_left, top); top += _scrollNotActive.height(); } // advanced top += st::setHeaderSkip; _connectionType.move(_left + st::setHeaderLeft + _connectionTypeWidth, top); top += _connectionType.height() + st::setLittleSkip; if (_self) { _resetSessions.move(_left, top); top += _resetSessions.height() + st::setSectionSkip; _logOut.move(_left, top); } }
void SettingsInner::paintEvent(QPaintEvent *e) { QPainter p(this); p.setClipRect(e->rect()); int32 top = 0; if (_self) { // profile top += st::setTop; _nameText.drawElided(p, _uploadPhoto.x() + st::setNameLeft, top + st::setNameTop, _uploadPhoto.width() - st::setNameLeft); if (!_cancelPhoto.isHidden()) { p.setFont(st::linkFont->f); p.setPen(st::black->p); p.drawText(_uploadPhoto.x() + st::setPhoneLeft, _cancelPhoto.y() + st::linkFont->ascent, lang(lng_settings_uploading_photo)); } p.setFont(st::setPhoneFont->f); p.setPen(st::setPhoneColor->p); p.drawText(_uploadPhoto.x() + st::setPhoneLeft, top + st::setPhoneTop + st::setPhoneFont->ascent, _phoneText); if (_photoLink) { p.drawPixmap(_left, top, _self->photo->pix(st::setPhotoSize)); } else { if (a_photo.current() < 1) { p.drawPixmap(QPoint(_left, top), App::sprite(), st::setPhotoImg); } if (a_photo.current() > 0) { p.setOpacity(a_photo.current()); p.drawPixmap(QPoint(_left, top), App::sprite(), st::setOverPhotoImg); p.setOpacity(1); } } top += st::setPhotoSize; if (!_errorText.isEmpty()) { p.setFont(st::setErrFont->f); p.setPen(st::setErrColor->p); p.drawText(QRect(_uploadPhoto.x(), _uploadPhoto.y() + _uploadPhoto.height() + st::setLittleSkip, _uploadPhoto.width(), st::setErrFont->height), _errorText, style::al_center); } // notifications p.setFont(st::setHeaderFont->f); p.setPen(st::setHeaderColor->p); p.drawText(_left + st::setHeaderLeft, top + st::setHeaderTop + st::setHeaderFont->ascent, lang(lng_settings_section_notify)); top += st::setHeaderSkip; top += _desktopNotify.height() + st::setLittleSkip; top += _soundNotify.height(); } // general p.setFont(st::setHeaderFont->f); p.setPen(st::setHeaderColor->p); p.drawText(_left + st::setHeaderLeft, top + st::setHeaderTop + st::setHeaderFont->ascent, lang(lng_settings_section_general)); top += st::setHeaderSkip; top += _autoUpdate.height(); QString textToDraw; if (cAutoUpdate()) { switch (_updatingState) { case UpdatingNone: textToDraw = _curVersionText; break; case UpdatingCheck: textToDraw = lang(lng_settings_update_checking); break; case UpdatingLatest: textToDraw = lang(lng_settings_latest_installed); break; case UpdatingDownload: textToDraw = _newVersionDownload; break; case UpdatingReady: textToDraw = _newVersionText; break; case UpdatingFail: textToDraw = lang(lng_settings_update_fail); break; } } else { textToDraw = _curVersionText; } p.setFont(st::linkFont->f); p.setPen(st::setVersionColor->p); p.drawText(_left + st::setVersionLeft, top + st::setVersionTop + st::linkFont->ascent, textToDraw); top += st::setVersionHeight; if (cPlatform() == dbipWindows) { top += _workmodeTray.height() + st::setLittleSkip; top += _workmodeWindow.height() + st::setSectionSkip; top += _autoStart.height() + st::setLittleSkip; top += _startMinimized.height(); } if (!cRetina()) { p.setFont(st::setHeaderFont->f); p.setPen(st::setHeaderColor->p); p.drawText(_left + st::setHeaderLeft, top + st::setHeaderTop + st::setHeaderFont->ascent, lang(lng_settings_scale_label)); top += st::setHeaderSkip; top += _dpiAutoScale.height() + st::setLittleSkip; top += _dpiSlider.height() + st::dpiFont4->height; int32 sLeft = _dpiSlider.x() + _dpiWidth1 / 2, sWidth = _dpiSlider.width(); float64 sStep = (sWidth - _dpiWidth1 / 2 - _dpiWidth4 / 2) / float64(dbisScaleCount - 2); p.setFont(st::dpiFont1->f); p.setPen((scaleIs(dbisOne) ? st::dpiActive : st::dpiInactive)->p); p.drawText(sLeft + qRound(0 * sStep) - _dpiWidth1 / 2, top - (st::dpiFont4->height - st::dpiFont1->height) / 2 - st::dpiFont1->descent, scaleLabel(dbisOne)); p.setFont(st::dpiFont2->f); p.setPen((scaleIs(dbisOneAndQuarter) ? st::dpiActive : st::dpiInactive)->p); p.drawText(sLeft + qRound(1 * sStep) - _dpiWidth2 / 2, top - (st::dpiFont4->height - st::dpiFont2->height) / 2 - st::dpiFont2->descent, scaleLabel(dbisOneAndQuarter)); p.setFont(st::dpiFont3->f); p.setPen((scaleIs(dbisOneAndHalf) ? st::dpiActive : st::dpiInactive)->p); p.drawText(sLeft + qRound(2 * sStep) - _dpiWidth3 / 2, top - (st::dpiFont4->height - st::dpiFont3->height) / 2 - st::dpiFont3->descent, scaleLabel(dbisOneAndHalf)); p.setFont(st::dpiFont4->f); p.setPen((scaleIs(dbisTwo) ? st::dpiActive : st::dpiInactive)->p); p.drawText(sLeft + qRound(3 * sStep) - _dpiWidth4 / 2, top - (st::dpiFont4->height - st::dpiFont4->height) / 2 - st::dpiFont4->descent, scaleLabel(dbisTwo)); p.setFont(st::linkFont->f); } if (_self) { // chat options p.setFont(st::setHeaderFont->f); p.setPen(st::setHeaderColor->p); p.drawText(_left + st::setHeaderLeft, top + st::setHeaderTop + st::setHeaderFont->ascent, lang(lng_settings_section_chat)); top += st::setHeaderSkip; top += _replaceEmojis.height() + st::setSectionSkip; top += _enterSend.height() + st::setLittleSkip; top += _ctrlEnterSend.height() + st::setSectionSkip; top += _dontAskDownloadPath.height(); if (!cAskDownloadPath()) { top += st::setLittleSkip; p.setFont(st::linkFont->f); p.setPen(st::black->p); p.drawText(_left + st::setVersionLeft, top + st::linkFont->ascent, lang(lng_download_path_label)); if (cDownloadPath().isEmpty()) { QString clearText; int32 clearWidth = 0; switch (_tempDirClearState) { case TempDirClearing: clearText = lang(lng_download_path_clearing); clearWidth = _tempDirClearingWidth; break; case TempDirCleared: clearText = lang(lng_download_path_cleared); clearWidth = _tempDirClearedWidth; break; case TempDirClearFailed: clearText = lang(lng_download_path_clear_failed); clearWidth = _tempDirClearFailedWidth; break; } if (clearWidth) { p.drawText(_left + st::setWidth - clearWidth, top + st::linkFont->ascent, clearText); } } top += _downloadPathEdit.height(); } top += st::setSectionSkip; top += _catsAndDogs.height() + st::setLittleSkip; top += _scrollNotActive.height(); } // advanced p.setFont(st::setHeaderFont->f); p.setPen(st::setHeaderColor->p); p.drawText(_left + st::setHeaderLeft, top + st::setHeaderTop + st::setHeaderFont->ascent, lang(lng_settings_section_advanced)); top += st::setHeaderSkip; p.setFont(st::linkFont->f); p.setPen(st::black->p); p.drawText(_left + st::setHeaderLeft, _connectionType.y() + st::linkFont->ascent, _connectionTypeText); if (_self && _resetDone) { p.drawText(_resetSessions.x(), _resetSessions.y() + st::linkFont->ascent, lang(lng_settings_reset_done)); } }
std::unique_ptr<Result> Result::create(uint64 queryId, const MTPBotInlineResult &mtpData) { using StringToTypeMap = QMap<QString, Result::Type>; static StaticNeverFreedPointer<StringToTypeMap> stringToTypeMap{ ([]() -> StringToTypeMap* { auto result = std::make_unique<StringToTypeMap>(); result->insert(qsl("photo"), Result::Type::Photo); result->insert(qsl("video"), Result::Type::Video); result->insert(qsl("audio"), Result::Type::Audio); result->insert(qsl("voice"), Result::Type::Audio); result->insert(qsl("sticker"), Result::Type::Sticker); result->insert(qsl("file"), Result::Type::File); result->insert(qsl("gif"), Result::Type::Gif); result->insert(qsl("article"), Result::Type::Article); result->insert(qsl("contact"), Result::Type::Contact); result->insert(qsl("venue"), Result::Type::Venue); result->insert(qsl("geo"), Result::Type::Geo); result->insert(qsl("game"), Result::Type::Game); return result.release(); })() }; auto getInlineResultType = [](const MTPBotInlineResult &inlineResult) -> Type { QString type; switch (inlineResult.type()) { case mtpc_botInlineResult: type = qs(inlineResult.c_botInlineResult().vtype); break; case mtpc_botInlineMediaResult: type = qs(inlineResult.c_botInlineMediaResult().vtype); break; } return stringToTypeMap->value(type, Type::Unknown); }; Type type = getInlineResultType(mtpData); if (type == Type::Unknown) { return nullptr; } auto result = std::make_unique<Result>(Creator{ queryId, type }); const MTPBotInlineMessage *message = nullptr; switch (mtpData.type()) { case mtpc_botInlineResult: { const auto &r = mtpData.c_botInlineResult(); result->_id = qs(r.vid); if (r.has_title()) result->_title = qs(r.vtitle); if (r.has_description()) result->_description = qs(r.vdescription); if (r.has_url()) result->_url = qs(r.vurl); if (r.has_thumb()) { result->_thumb = ImagePtr(r.vthumb, result->thumbBox()); } if (r.has_content()) { result->_content_url = GetContentUrl(r.vcontent); if (result->_type == Type::Photo) { result->_photo = Auth().data().photoFromWeb( r.vcontent, result->_thumb); } else { result->_document = Auth().data().documentFromWeb( result->adjustAttributes(r.vcontent), result->_thumb); } } message = &r.vsend_message; } break; case mtpc_botInlineMediaResult: { const auto &r = mtpData.c_botInlineMediaResult(); result->_id = qs(r.vid); if (r.has_title()) result->_title = qs(r.vtitle); if (r.has_description()) result->_description = qs(r.vdescription); if (r.has_photo()) { result->_photo = Auth().data().photo(r.vphoto); } if (r.has_document()) { result->_document = Auth().data().document(r.vdocument); } message = &r.vsend_message; } break; } auto badAttachment = (result->_photo && result->_photo->full->isNull()) || (result->_document && !result->_document->isValid()); if (!message) { return nullptr; } // Ensure required media fields for layouts. if (result->_type == Type::Photo) { if (!result->_photo) { return nullptr; } } else if (result->_type == Type::Audio || result->_type == Type::File || result->_type == Type::Video || result->_type == Type::Sticker || result->_type == Type::Gif) { if (!result->_document) { return nullptr; } } switch (message->type()) { case mtpc_botInlineMessageMediaAuto: { auto &r = message->c_botInlineMessageMediaAuto(); auto entities = r.has_entities() ? TextUtilities::EntitiesFromMTP(r.ventities.v) : EntitiesInText(); if (result->_type == Type::Photo) { if (!result->_photo) { return nullptr; } result->sendData = std::make_unique<internal::SendPhoto>(result->_photo, qs(r.vmessage), entities); } else if (result->_type == Type::Game) { result->createGame(); result->sendData = std::make_unique<internal::SendGame>(result->_game); } else { if (!result->_document) { return nullptr; } result->sendData = std::make_unique<internal::SendFile>(result->_document, qs(r.vmessage), entities); } if (r.has_reply_markup()) { result->_mtpKeyboard = std::make_unique<MTPReplyMarkup>(r.vreply_markup); } } break; case mtpc_botInlineMessageText: { auto &r = message->c_botInlineMessageText(); auto entities = r.has_entities() ? TextUtilities::EntitiesFromMTP(r.ventities.v) : EntitiesInText(); result->sendData = std::make_unique<internal::SendText>(qs(r.vmessage), entities, r.is_no_webpage()); if (result->_type == Type::Photo) { if (!result->_photo) { return nullptr; } } else if (result->_type == Type::Audio || result->_type == Type::File || result->_type == Type::Video || result->_type == Type::Sticker || result->_type == Type::Gif) { if (!result->_document) { return nullptr; } } if (r.has_reply_markup()) { result->_mtpKeyboard = std::make_unique<MTPReplyMarkup>(r.vreply_markup); } } break; case mtpc_botInlineMessageMediaGeo: { // #TODO layer 72 save period and send live location?.. auto &r = message->c_botInlineMessageMediaGeo(); if (r.vgeo.type() == mtpc_geoPoint) { result->sendData = std::make_unique<internal::SendGeo>(r.vgeo.c_geoPoint()); } else { badAttachment = true; } if (r.has_reply_markup()) { result->_mtpKeyboard = std::make_unique<MTPReplyMarkup>(r.vreply_markup); } } break; case mtpc_botInlineMessageMediaVenue: { auto &r = message->c_botInlineMessageMediaVenue(); if (r.vgeo.type() == mtpc_geoPoint) { result->sendData = std::make_unique<internal::SendVenue>(r.vgeo.c_geoPoint(), qs(r.vvenue_id), qs(r.vprovider), qs(r.vtitle), qs(r.vaddress)); } else { badAttachment = true; } if (r.has_reply_markup()) { result->_mtpKeyboard = std::make_unique<MTPReplyMarkup>(r.vreply_markup); } } break; case mtpc_botInlineMessageMediaContact: { auto &r = message->c_botInlineMessageMediaContact(); result->sendData = std::make_unique<internal::SendContact>(qs(r.vfirst_name), qs(r.vlast_name), qs(r.vphone_number)); if (r.has_reply_markup()) { result->_mtpKeyboard = std::make_unique<MTPReplyMarkup>(r.vreply_markup); } } break; default: { badAttachment = true; } break; } if (badAttachment || !result->sendData || !result->sendData->isValid()) { return nullptr; } LocationCoords location; if (result->getLocationCoords(&location)) { int32 w = st::inlineThumbSize, h = st::inlineThumbSize; int32 zoom = 13, scale = 1; if (cScale() == dbisTwo || cRetina()) { scale = 2; w /= 2; h /= 2; } auto coords = location.latAsString() + ',' + location.lonAsString(); QString url = qsl("https://maps.googleapis.com/maps/api/staticmap?center=") + coords + qsl("&zoom=%1&size=%2x%3&maptype=roadmap&scale=%4&markers=color:red|size:big|").arg(zoom).arg(w).arg(h).arg(scale) + coords + qsl("&sensor=false"); result->_locationThumb = ImagePtr(url); } return result; }
void FlatTextarea::processDocumentContentsChange(int position, int charsAdded) { int32 replacePosition = -1, replaceLen = 0; const EmojiData *emoji = 0; static QString regular = qsl("Open Sans"), semibold = qsl("Open Sans Semibold"); bool checkTilde = !cRetina() && (font().pixelSize() == 13) && (font().family() == regular), wasTildeFragment = false; QTextDocument *doc(document()); while (true) { int32 start = position, end = position + charsAdded; QTextBlock from = doc->findBlock(start), till = doc->findBlock(end); if (till.isValid()) till = till.next(); for (QTextBlock b = from; b != till; b = b.next()) { for (QTextBlock::Iterator iter = b.begin(); !iter.atEnd(); ++iter) { QTextFragment fragment(iter.fragment()); if (!fragment.isValid()) continue; int32 fp = fragment.position(), fe = fp + fragment.length(); if (fp >= end || fe <= start) { continue; } if (checkTilde) { wasTildeFragment = (fragment.charFormat().fontFamily() == semibold); } QString t(fragment.text()); const QChar *ch = t.constData(), *e = ch + t.size(); for (; ch != e; ++ch, ++fp) { int32 emojiLen = 0; emoji = emojiFromText(ch, e, &emojiLen); if (emoji) { if (replacePosition >= 0) { emoji = 0; // replace tilde char format first } else { replacePosition = fp; replaceLen = emojiLen; } break; } if (checkTilde && fp >= position) { // tilde fix in OpenSans bool tilde = (ch->unicode() == '~'); if ((tilde && !wasTildeFragment) || (!tilde && wasTildeFragment)) { if (replacePosition < 0) { replacePosition = fp; replaceLen = 1; } else { ++replaceLen; } } else if (replacePosition >= 0) { break; } } if (ch + 1 < e && ch->isHighSurrogate() && (ch + 1)->isLowSurrogate()) { ++ch; ++fp; } } if (replacePosition >= 0) break; } if (replacePosition >= 0) break; } if (replacePosition >= 0) { if (!document()->pageSize().isNull()) { document()->setPageSize(QSizeF(0, 0)); } QTextCursor c(doc->docHandle(), replacePosition); c.setPosition(replacePosition + replaceLen, QTextCursor::KeepAnchor); if (emoji) { insertEmoji(emoji, c); } else { QTextCharFormat format; format.setFontFamily(wasTildeFragment ? regular : semibold); c.mergeCharFormat(format); } charsAdded -= replacePosition + replaceLen - position; position = replacePosition + (emoji ? 1 : replaceLen); emoji = 0; replacePosition = -1; } else { break; } } }
std_::unique_ptr<Result> Result::create(uint64 queryId, const MTPBotInlineResult &mtpData) { using StringToTypeMap = QMap<QString, Result::Type>; static StaticNeverFreedPointer<StringToTypeMap> stringToTypeMap{ ([]() -> StringToTypeMap* { auto result = std_::make_unique<StringToTypeMap>(); result->insert(qsl("photo"), Result::Type::Photo); result->insert(qsl("video"), Result::Type::Video); result->insert(qsl("audio"), Result::Type::Audio); result->insert(qsl("voice"), Result::Type::Audio); result->insert(qsl("sticker"), Result::Type::Sticker); result->insert(qsl("file"), Result::Type::File); result->insert(qsl("gif"), Result::Type::Gif); result->insert(qsl("article"), Result::Type::Article); result->insert(qsl("contact"), Result::Type::Contact); result->insert(qsl("venue"), Result::Type::Venue); result->insert(qsl("geo"), Result::Type::Geo); result->insert(qsl("game"), Result::Type::Game); return result.release(); })() }; auto getInlineResultType = [](const MTPBotInlineResult &inlineResult) -> Type { QString type; switch (inlineResult.type()) { case mtpc_botInlineResult: type = qs(inlineResult.c_botInlineResult().vtype); break; case mtpc_botInlineMediaResult: type = qs(inlineResult.c_botInlineMediaResult().vtype); break; } return stringToTypeMap->value(type, Type::Unknown); }; Type type = getInlineResultType(mtpData); if (type == Type::Unknown) { return std_::unique_ptr<Result>(); } auto result = std_::make_unique<Result>(Creator{ queryId, type }); const MTPBotInlineMessage *message = nullptr; switch (mtpData.type()) { case mtpc_botInlineResult: { const auto &r(mtpData.c_botInlineResult()); result->_id = qs(r.vid); if (r.has_title()) result->_title = qs(r.vtitle); if (r.has_description()) result->_description = qs(r.vdescription); if (r.has_url()) result->_url = qs(r.vurl); if (r.has_thumb_url()) result->_thumb_url = qs(r.vthumb_url); if (r.has_content_type()) result->_content_type = qs(r.vcontent_type); if (r.has_content_url()) result->_content_url = qs(r.vcontent_url); if (r.has_w()) result->_width = r.vw.v; if (r.has_h()) result->_height = r.vh.v; if (r.has_duration()) result->_duration = r.vduration.v; if (!result->_thumb_url.startsWith(qstr("http://"), Qt::CaseInsensitive) && !result->_thumb_url.startsWith(qstr("https://"), Qt::CaseInsensitive)) { result->_thumb_url = QString(); } message = &r.vsend_message; } break; case mtpc_botInlineMediaResult: { const auto &r(mtpData.c_botInlineMediaResult()); result->_id = qs(r.vid); if (r.has_title()) result->_title = qs(r.vtitle); if (r.has_description()) result->_description = qs(r.vdescription); if (r.has_photo()) { result->_photo = App::feedPhoto(r.vphoto); } if (r.has_document()) { result->_document = App::feedDocument(r.vdocument); } message = &r.vsend_message; } break; } bool badAttachment = (result->_photo && !result->_photo->access) || (result->_document && !result->_document->isValid()); if (!message) { return std_::unique_ptr<Result>(); } // Ensure required media fields for layouts. if (result->_type == Type::Photo) { if (!result->_photo && result->_content_url.isEmpty()) { return std_::unique_ptr<Result>(); } result->createPhoto(); } else if (result->_type == Type::File || result->_type == Type::Gif || result->_type == Type::Sticker) { if (!result->_document && result->_content_url.isEmpty()) { return std_::unique_ptr<Result>(); } result->createDocument(); } switch (message->type()) { case mtpc_botInlineMessageMediaAuto: { auto &r = message->c_botInlineMessageMediaAuto(); if (result->_type == Type::Photo) { result->createPhoto(); result->sendData.reset(new internal::SendPhoto(result->_photo, qs(r.vcaption))); } else if (result->_type == Type::Game) { result->createGame(); result->sendData.reset(new internal::SendGame(result->_game)); } else { result->createDocument(); result->sendData.reset(new internal::SendFile(result->_document, qs(r.vcaption))); } if (r.has_reply_markup()) { result->_mtpKeyboard = std_::make_unique<MTPReplyMarkup>(r.vreply_markup); } } break; case mtpc_botInlineMessageText: { auto &r = message->c_botInlineMessageText(); EntitiesInText entities = r.has_entities() ? entitiesFromMTP(r.ventities.c_vector().v) : EntitiesInText(); result->sendData.reset(new internal::SendText(qs(r.vmessage), entities, r.is_no_webpage())); if (result->_type == Type::Photo) { result->createPhoto(); } else if (result->_type == Type::Audio || result->_type == Type::File || result->_type == Type::Video || result->_type == Type::Sticker || result->_type == Type::Gif) { result->createDocument(); } if (r.has_reply_markup()) { result->_mtpKeyboard = std_::make_unique<MTPReplyMarkup>(r.vreply_markup); } } break; case mtpc_botInlineMessageMediaGeo: { auto &r = message->c_botInlineMessageMediaGeo(); if (r.vgeo.type() == mtpc_geoPoint) { result->sendData.reset(new internal::SendGeo(r.vgeo.c_geoPoint())); } else { badAttachment = true; } if (r.has_reply_markup()) { result->_mtpKeyboard = std_::make_unique<MTPReplyMarkup>(r.vreply_markup); } } break; case mtpc_botInlineMessageMediaVenue: { auto &r = message->c_botInlineMessageMediaVenue(); if (r.vgeo.type() == mtpc_geoPoint) { result->sendData.reset(new internal::SendVenue(r.vgeo.c_geoPoint(), qs(r.vvenue_id), qs(r.vprovider), qs(r.vtitle), qs(r.vaddress))); } else { badAttachment = true; } if (r.has_reply_markup()) { result->_mtpKeyboard = std_::make_unique<MTPReplyMarkup>(r.vreply_markup); } } break; case mtpc_botInlineMessageMediaContact: { auto &r = message->c_botInlineMessageMediaContact(); result->sendData.reset(new internal::SendContact(qs(r.vfirst_name), qs(r.vlast_name), qs(r.vphone_number))); if (r.has_reply_markup()) { result->_mtpKeyboard = std_::make_unique<MTPReplyMarkup>(r.vreply_markup); } } break; default: { badAttachment = true; } break; } if (badAttachment || !result->sendData || !result->sendData->isValid()) { return std_::unique_ptr<Result>(); } if (result->_thumb->isNull() && !result->_thumb_url.isEmpty()) { result->_thumb = ImagePtr(result->_thumb_url); } LocationCoords location; if (result->getLocationCoords(&location)) { int32 w = st::inlineThumbSize, h = st::inlineThumbSize; int32 zoom = 13, scale = 1; if (cScale() == dbisTwo || cRetina()) { scale = 2; w /= 2; h /= 2; } QString coords = qsl("%1,%2").arg(location.lat).arg(location.lon); QString url = qsl("https://maps.googleapis.com/maps/api/staticmap?center=") + coords + qsl("&zoom=%1&size=%2x%3&maptype=roadmap&scale=%4&markers=color:red|size:big|").arg(zoom).arg(w).arg(h).arg(scale) + coords + qsl("&sensor=false"); result->_locationThumb = ImagePtr(url); } return result; }