diff options
Diffstat (limited to 'protocols/NewsAggregator/Src/Options.cpp')
-rw-r--r-- | protocols/NewsAggregator/Src/Options.cpp | 600 |
1 files changed, 2 insertions, 598 deletions
diff --git a/protocols/NewsAggregator/Src/Options.cpp b/protocols/NewsAggregator/Src/Options.cpp index c4c98d16ca..b133dd5b2d 100644 --- a/protocols/NewsAggregator/Src/Options.cpp +++ b/protocols/NewsAggregator/Src/Options.cpp @@ -19,594 +19,6 @@ Boston, MA 02111-1307, USA. #include "stdafx.h" -CExportFeed::CExportFeed() - : CSuper(g_plugin, IDD_FEEDEXPORT), - m_feedslist(this, IDC_FEEDSLIST), m_feedsexportlist(this, IDC_FEEDSEXPORTLIST), - m_addfeed(this, IDC_ADDFEED), m_removefeed(this, IDC_REMOVEFEED), - m_addallfeeds(this, IDC_ADDALLFEEDS), m_removeallfeeds(this, IDC_REMOVEALLFEEDS), - m_ok(this, IDOK) -{ - m_addfeed.OnClick = Callback(this, &CExportFeed::OnAddFeed); - m_removefeed.OnClick = Callback(this, &CExportFeed::OnRemoveFeed); - m_addallfeeds.OnClick = Callback(this, &CExportFeed::OnAddAllFeeds); - m_removeallfeeds.OnClick = Callback(this, &CExportFeed::OnRemoveAllFeeds); - - m_feedslist.OnDblClick = Callback(this, &CExportFeed::OnFeedsList); - m_feedsexportlist.OnDblClick = Callback(this, &CExportFeed::OnFeedsExportList); -} - -bool CExportFeed::OnInitDialog() -{ - Utils_RestoreWindowPositionNoSize(m_hwnd, NULL, MODULENAME, "ExportDlg"); - for (auto &hContact : Contacts(MODULENAME)) { - ptrW message(g_plugin.getWStringA(hContact, "Nick")); - if (message != nullptr) - m_feedslist.AddString(message); - } - m_removefeed.Disable(); - m_removeallfeeds.Disable(); - m_ok.Disable(); - if (!m_feedslist.GetCount()) { - m_addfeed.Disable(); - m_addallfeeds.Disable(); - } - return true; -} - -void CExportFeed::OnAddFeed(CCtrlBase*) -{ - if (!m_removefeed.Enabled()) - m_removefeed.Enable(); - if (!m_removeallfeeds.Enabled()) - m_removeallfeeds.Enable(); - if (!m_ok.Enabled()) - m_ok.Enable(); - int cursel = m_feedslist.GetCurSel(); - wchar_t item[MAX_PATH]; - m_feedslist.GetItemText(cursel, item, _countof(item)); - m_feedsexportlist.AddString(item); - m_feedslist.DeleteString(cursel); - if (!m_feedslist.GetCount()) { - m_addfeed.Disable(); - m_addallfeeds.Disable(); - } -} - -void CExportFeed::OnRemoveFeed(CCtrlBase*) -{ - if (!m_addfeed.Enabled()) - m_addfeed.Enable(); - if (!m_addallfeeds.Enabled()) - m_addallfeeds.Enable(); - int cursel = m_feedsexportlist.GetCurSel(); - wchar_t item[MAX_PATH]; - m_feedsexportlist.GetItemText(cursel, item, _countof(item)); - m_feedslist.AddString(item); - m_feedsexportlist.DeleteString(cursel); - if (!m_feedsexportlist.GetCount()) { - m_removefeed.Disable(); - m_removeallfeeds.Disable(); - m_ok.Disable(); - } -} - -void CExportFeed::OnAddAllFeeds(CCtrlBase*) -{ - if (!m_removefeed.Enabled()) - m_removefeed.Enable(); - if (!m_removeallfeeds.Enabled()) - m_removeallfeeds.Enable(); - if (!m_ok.Enabled()) - m_ok.Enable(); - int count = m_feedslist.GetCount(); - for (int i = 0; i < count; i++) { - wchar_t item[MAX_PATH]; - m_feedslist.GetItemText(i, item, _countof(item)); - m_feedsexportlist.AddString(item); - } - for (int i = count - 1; i > -1; i--) - m_feedslist.DeleteString(i); - m_addfeed.Disable(); - m_addallfeeds.Disable(); -} - -void CExportFeed::OnRemoveAllFeeds(CCtrlBase*) -{ - if (!m_addfeed.Enabled()) - m_addfeed.Enable(); - if (!m_addallfeeds.Enabled()) - m_addallfeeds.Enable(); - int count = m_feedsexportlist.GetCount(); - for (int i = 0; i < count; i++) { - wchar_t item[MAX_PATH]; - m_feedsexportlist.GetItemText(i, item, _countof(item)); - m_feedslist.AddString(item); - } - for (int i = count - 1; i > -1; i--) - m_feedsexportlist.DeleteString(i); - m_removefeed.Disable(); - m_removeallfeeds.Disable(); - m_ok.Disable(); -} - -void CExportFeed::OnFeedsList(CCtrlBase*) -{ - if (!m_removefeed.Enabled()) - m_removefeed.Enable(); - if (!m_removeallfeeds.Enabled()) - m_removeallfeeds.Enable(); - if (!m_ok.Enabled()) - m_ok.Enable(); - int cursel = m_feedslist.GetCurSel(); - wchar_t item[MAX_PATH]; - m_feedslist.GetItemText(cursel, item, _countof(item)); - m_feedsexportlist.AddString(item); - m_feedslist.DeleteString(cursel); - if (!m_feedslist.GetCount()) { - m_addfeed.Disable(); - m_addallfeeds.Disable(); - } -} - -void CExportFeed::OnFeedsExportList(CCtrlBase*) -{ - if (!m_addfeed.Enabled()) - m_addfeed.Enable(); - if (!m_addallfeeds.Enabled()) - m_addallfeeds.Enable(); - int cursel = m_feedsexportlist.GetCurSel(); - wchar_t item[MAX_PATH]; - m_feedsexportlist.GetItemText(cursel, item, _countof(item)); - m_feedslist.AddString(item); - m_feedsexportlist.DeleteString(cursel); - if (!m_feedsexportlist.GetCount()) { - m_removefeed.Disable(); - m_removeallfeeds.Disable(); - m_ok.Disable(); - } -} - -static const TiXmlElement* AdviceNode(const TiXmlElement *node) -{ - auto *tmpnode = node; - - // try to rest on the same level first - node = node->NextSiblingElement(); - if (node) - return node; - - do { - // go up one level - node = tmpnode->Parent()->ToElement(); - tmpnode = node; - node = node->NextSiblingElement(); - if (node) - return node; - } - while (mir_strcmpi(tmpnode->Name(), "body")); - - // nothing found or we reached body - return nullptr; -} - -bool CExportFeed::OnApply() -{ - wchar_t FileName[MAX_PATH]; - VARSW tszMirDir(L"%miranda_path%"); - - OPENFILENAME ofn = { 0 }; - ofn.lStructSize = sizeof(ofn); - wchar_t tmp[MAX_PATH]; - mir_snwprintf(tmp, L"%s (*.opml)%c*.opml%c%c", TranslateT("OPML files"), 0, 0, 0); - ofn.lpstrFilter = tmp; - ofn.hwndOwner = nullptr; - ofn.lpstrFile = FileName; - ofn.nMaxFile = MAX_PATH; - ofn.nMaxFileTitle = MAX_PATH; - ofn.Flags = OFN_HIDEREADONLY | OFN_SHAREAWARE | OFN_PATHMUSTEXIST | OFN_OVERWRITEPROMPT; - ofn.lpstrInitialDir = tszMirDir; - *FileName = '\0'; - ofn.lpstrDefExt = L""; - if (!GetSaveFileName(&ofn)) - return false; - - TiXmlDocument doc; - auto *hXml = doc.NewElement("opml"); doc.InsertEndChild(hXml); - hXml->SetAttribute("version", "1.0"); - - auto *xmlHeader = doc.NewElement("head"); hXml->InsertEndChild(xmlHeader); - auto *xmlTitle = doc.NewElement("title"); xmlTitle->SetText("Miranda NG NewsAggregator plugin export"); xmlHeader->InsertEndChild(xmlTitle); - - auto *xmlBody = doc.NewElement("body"); hXml->InsertEndChild(xmlBody); - - int count = m_feedsexportlist.GetCount(); - for (int i = 0; i < count; i++) { - wchar_t item[MAX_PATH]; - m_feedsexportlist.GetItemText(i, item, _countof(item)); - MCONTACT hContact = GetContactByNick(item); - wchar_t - *title = g_plugin.getWStringA(hContact, "Nick"), - *url = g_plugin.getWStringA(hContact, "URL"), - *siteurl = g_plugin.getWStringA(hContact, "Homepage"), - *group = Clist_GetGroup(hContact); - - TiXmlElement *elem = xmlBody; - if (group) { - wchar_t *section = wcstok(group, L"\\"); - while (section != nullptr) { - TiXmlElement *existgroup = 0; - for (auto *it : TiXmlFilter(elem, "outline")) { - if (it->Attribute("title", T2Utf(section))) { - existgroup = (TiXmlElement*)it; - break; - } - } - - if (!existgroup) { - auto *pNew = doc.NewElement("outline"); - pNew->SetAttribute("title", section); pNew->SetAttribute("text", section); - elem->InsertEndChild(pNew); - elem = pNew; - } - else elem = existgroup; - - section = wcstok(nullptr, L"\\"); - } - } - - auto *pNew = doc.NewElement("outline"); elem->InsertEndChild(pNew); - pNew->SetAttribute("text", title); - pNew->SetAttribute("title", title); - pNew->SetAttribute("type", "rss"); - pNew->SetAttribute("xmlUrl", url); - pNew->SetAttribute("htmlUrl", siteurl); - - mir_free(title); - mir_free(url); - mir_free(siteurl); - mir_free(group); - } - - FILE *out = _wfopen(FileName, L"wb"); - if (out) { - tinyxml2::XMLPrinter printer(out); - doc.Print(&printer); - fclose(out); - } - return true; -} - -void CExportFeed::OnDestroy() -{ - Utils_SaveWindowPosition(m_hwnd, NULL, MODULENAME, "ExportDlg"); - if (pExportDialog) - pExportDialog = nullptr; -} - -///////////////////////////////////////////////////////////////////////////////////////// - -CImportFeed::CImportFeed(CCtrlListView *m_feeds) - : CSuper(g_plugin, IDD_FEEDIMPORT), - m_importfile(this, IDC_IMPORTFILEPATH), m_browsefile(this, IDC_BROWSEIMPORTFILE), - m_feedslist(this, IDC_FEEDSLIST), m_feedsimportlist(this, IDC_FEEDSIMPORTLIST), - m_addfeed(this, IDC_ADDFEED), m_removefeed(this, IDC_REMOVEFEED), - m_addallfeeds(this, IDC_ADDALLFEEDS), m_removeallfeeds(this, IDC_REMOVEALLFEEDS), - m_ok(this, IDOK) -{ - m_list = m_feeds; - m_browsefile.OnClick = Callback(this, &CImportFeed::OnBrowseFile); - m_addfeed.OnClick = Callback(this, &CImportFeed::OnAddFeed); - m_removefeed.OnClick = Callback(this, &CImportFeed::OnRemoveFeed); - m_addallfeeds.OnClick = Callback(this, &CImportFeed::OnAddAllFeeds); - m_removeallfeeds.OnClick = Callback(this, &CImportFeed::OnRemoveAllFeeds); - - m_feedslist.OnDblClick = Callback(this, &CImportFeed::OnFeedsList); - m_feedsimportlist.OnDblClick = Callback(this, &CImportFeed::OnFeedsImportList); -} - -bool CImportFeed::OnInitDialog() -{ - Utils_RestoreWindowPositionNoSize(m_hwnd, NULL, MODULENAME, "ImportDlg"); - m_removefeed.Disable(); - m_removeallfeeds.Disable(); - m_ok.Disable(); - m_addfeed.Disable(); - m_addallfeeds.Disable(); - return true; -} - -void CImportFeed::OnBrowseFile(CCtrlBase*) -{ - wchar_t FileName[MAX_PATH]; - VARSW tszMirDir(L"%miranda_path%"); - - OPENFILENAME ofn = { 0 }; - ofn.lStructSize = sizeof(ofn); - wchar_t tmp[MAX_PATH]; - mir_snwprintf(tmp, L"%s (*.opml, *.xml)%c*.opml;*.xml%c%c", TranslateT("OPML files"), 0, 0, 0); - ofn.lpstrFilter = tmp; - ofn.hwndOwner = nullptr; - ofn.lpstrFile = FileName; - ofn.nMaxFile = MAX_PATH; - ofn.nMaxFileTitle = MAX_PATH; - ofn.Flags = OFN_HIDEREADONLY; - ofn.lpstrInitialDir = tszMirDir; - *FileName = '\0'; - ofn.lpstrDefExt = L""; - if (!GetOpenFileName(&ofn)) - return; - - FILE *in = _wfopen(FileName, L"rb"); - if (in == nullptr) - return; - - TiXmlDocument doc; - int res = doc.LoadFile(in); - fclose(in); - if (res != 0) { - MessageBox(m_hwnd, TranslateT("Not valid import file."), TranslateT("Error"), MB_OK | MB_ICONERROR); - return; - } - - m_importfile.SetText(FileName); - - auto *node = TiXmlConst(&doc)["opml"]["body"]["outline"].ToElement(); - if (!node) - node = TiXmlConst(&doc)["body"]["outline"].ToElement(); - if (node == nullptr) { - MessageBox(m_hwnd, TranslateT("Not valid import file."), TranslateT("Error"), MB_OK | MB_ICONERROR); - return; - } - - while (node) { - auto *pszUrl = node->Attribute("xmlUrl"); - if (!pszUrl && node->NoChildren()) - node = AdviceNode(node); - else if (!pszUrl && !node->NoChildren()) - node = node->FirstChildElement(); - else if (pszUrl) { - if (auto *pszText = node->Attribute("text")) { - Utf2T text(pszText); - m_feedslist.AddString(text); - m_addfeed.Enable(); - m_addallfeeds.Enable(); - } - - node = AdviceNode(node); - } - } -} - -void CImportFeed::OnAddFeed(CCtrlBase*) -{ - if (!m_removefeed.Enabled()) - m_removefeed.Enable(); - if (!m_removeallfeeds.Enabled()) - m_removeallfeeds.Enable(); - if (!m_ok.Enabled()) - m_ok.Enable(); - int cursel = m_feedslist.GetCurSel(); - wchar_t item[MAX_PATH]; - m_feedslist.GetItemText(cursel, item, _countof(item)); - m_feedsimportlist.AddString(item); - m_feedslist.DeleteString(cursel); - if (!m_feedslist.GetCount()) { - m_addfeed.Disable(); - m_addallfeeds.Disable(); - } -} - -void CImportFeed::OnRemoveFeed(CCtrlBase*) -{ - if (!m_addfeed.Enabled()) - m_addfeed.Enable(); - if (!m_addallfeeds.Enabled()) - m_addallfeeds.Enable(); - int cursel = m_feedsimportlist.GetCurSel(); - wchar_t item[MAX_PATH]; - m_feedsimportlist.GetItemText(cursel, item, _countof(item)); - m_feedslist.AddString(item); - m_feedsimportlist.DeleteString(cursel); - if (!m_feedsimportlist.GetCount()) { - m_removefeed.Disable(); - m_removeallfeeds.Disable(); - m_ok.Disable(); - } -} - -void CImportFeed::OnAddAllFeeds(CCtrlBase*) -{ - if (!m_removefeed.Enabled()) - m_removefeed.Enable(); - if (!m_removeallfeeds.Enabled()) - m_removeallfeeds.Enable(); - if (!m_ok.Enabled()) - m_ok.Enable(); - int count = m_feedslist.GetCount(); - for (int i = 0; i < count; i++) { - wchar_t item[MAX_PATH]; - m_feedslist.GetItemText(i, item, _countof(item)); - m_feedsimportlist.AddString(item); - } - for (int i = count - 1; i > -1; i--) - m_feedslist.DeleteString(i); - m_addfeed.Disable(); - m_addallfeeds.Disable(); -} - -void CImportFeed::OnRemoveAllFeeds(CCtrlBase*) -{ - if (!m_addfeed.Enabled()) - m_addfeed.Enable(); - if (!m_addallfeeds.Enabled()) - m_addallfeeds.Enable(); - int count = m_feedsimportlist.GetCount(); - for (int i = 0; i < count; i++) { - wchar_t item[MAX_PATH]; - m_feedsimportlist.GetItemText(i, item, _countof(item)); - m_feedslist.AddString(item); - } - for (int i = count - 1; i > -1; i--) - m_feedsimportlist.DeleteString(i); - m_removefeed.Disable(); - m_removeallfeeds.Disable(); - m_ok.Disable(); -} - -void CImportFeed::OnFeedsList(CCtrlBase*) -{ - if (!m_removefeed.Enabled()) - m_removefeed.Enable(); - if (!m_removeallfeeds.Enabled()) - m_removeallfeeds.Enable(); - if (!m_ok.Enabled()) - m_ok.Enable(); - int cursel = m_feedslist.GetCurSel(); - wchar_t item[MAX_PATH]; - m_feedslist.GetItemText(cursel, item, _countof(item)); - m_feedsimportlist.AddString(item); - m_feedslist.DeleteString(cursel); - if (!m_feedslist.GetCount()) { - m_addfeed.Disable(); - m_addallfeeds.Disable(); - } -} - -void CImportFeed::OnFeedsImportList(CCtrlBase*) -{ - if (!m_addfeed.Enabled()) - m_addfeed.Enable(); - if (!m_addallfeeds.Enabled()) - m_addallfeeds.Enable(); - int cursel = m_feedsimportlist.GetCurSel(); - wchar_t item[MAX_PATH]; - m_feedsimportlist.GetItemText(cursel, item, _countof(item)); - m_feedslist.AddString(item); - m_feedsimportlist.DeleteString(cursel); - if (!m_feedsimportlist.GetCount()) { - m_removefeed.Disable(); - m_removeallfeeds.Disable(); - m_ok.Disable(); - } -} - -bool CImportFeed::OnApply() -{ - wchar_t FileName[MAX_PATH]; - m_importfile.GetText(FileName, _countof(FileName)); - - FILE *in = _wfopen(FileName, L"rb"); - if (in == nullptr) - return false; - - TiXmlDocument doc; - int res = doc.LoadFile(in); - fclose(in); - if (res != 0) - return false; - - auto *node = TiXmlConst(&doc)["opml"]["body"]["outline"].ToElement(); - if (!node) - node = TiXmlConst(&doc)["body"]["outline"].ToElement(); - if (node == nullptr) - return false; - - int count = m_feedsimportlist.GetCount(); - int DUPES = 0; - - while (node) { - auto *pszUrl = node->Attribute("xmlUrl"); - if (!pszUrl && node->NoChildren()) - node = AdviceNode(node); - else if (!pszUrl && !node->NoChildren()) - node = node->FirstChildElement(); - else if (pszUrl) { - wchar_t *text = nullptr, *url = nullptr, *siteurl = nullptr; - bool bNeedToImport = false; - - if (auto *pszText = node->Attribute("text")) { - text = mir_utf8decodeW(pszText); - - for (int j = 0; j < count; j++) { - wchar_t item[MAX_PATH]; - m_feedsimportlist.GetItemText(j, item, _countof(item)); - if (!mir_wstrcmpi(item, text)) { - bNeedToImport = true; - break; - } - } - } - - if (auto *pszText = node->Attribute("xmlUrl")) { - url = mir_utf8decodeW(pszText); - if (GetContactByURL(url) && bNeedToImport) { - bNeedToImport = false; - DUPES++; - } - } - - if (auto *pszText = node->Attribute("htmlUrl")) - siteurl = mir_utf8decodeW(pszText); - - if (bNeedToImport && text && url) { - CMStringW wszGroup; - auto *parent = node->Parent()->ToElement(); - while (mir_strcmpi(parent->Name(), "body")) { - if (auto *pszText = parent->Attribute("text")) { - if (!wszGroup.IsEmpty()) - wszGroup.Insert(0, L"\\"); - wszGroup.Insert(0, Utf2T(pszText)); - } - parent = parent->Parent()->ToElement(); - } - - MCONTACT hContact = db_add_contact(); - Proto_AddToContact(hContact, MODULENAME); - Contact::Readonly(hContact); - g_plugin.setWString(hContact, "Nick", text); - g_plugin.setWString(hContact, "URL", url); - if (siteurl) - g_plugin.setWString(hContact, "Homepage", siteurl); - g_plugin.setByte(hContact, "CheckState", 1); - g_plugin.setDword(hContact, "UpdateTime", DEFAULT_UPDATE_TIME); - g_plugin.setWString(hContact, "MsgFormat", TAGSDEFAULT); - g_plugin.setWord(hContact, "Status", Proto_GetStatus(MODULENAME)); - - if (m_list != nullptr) { - int iItem = m_list->AddItem(text, -1); - m_list->SetItem(iItem, 1, url); - m_list->SetCheckState(iItem, 1); - } - - if (!wszGroup.IsEmpty()) { - Clist_GroupCreate(0, wszGroup); - Clist_SetGroup(hContact, wszGroup); - } - } - mir_free(text); - mir_free(url); - mir_free(siteurl); - - node = AdviceNode(node); - } - } - - wchar_t mes[MAX_PATH]; - if (DUPES) - mir_snwprintf(mes, TranslateT("Imported %d feed(s)\r\nNot imported %d duplicate(s)."), count - DUPES, DUPES); - else - mir_snwprintf(mes, TranslateT("Imported %d feed(s)."), count); - MessageBox(m_hwnd, mes, TranslateT("News Aggregator"), MB_OK | MB_ICONINFORMATION); - return true; -} - -void CImportFeed::OnDestroy() -{ - Utils_SaveWindowPosition(m_hwnd, NULL, MODULENAME, "ImportDlg"); - if (pImportDialog) - pImportDialog = nullptr; -} - ///////////////////////////////////////////////////////////////////////////////////////// CFeedEditor::CFeedEditor(int iItem, CCtrlListView *m_feeds, MCONTACT Contact) @@ -964,20 +376,12 @@ void COptionsMain::OnDeleteButtonClick(CCtrlBase*) void COptionsMain::OnImportButtonClick(CCtrlBase*) { - if (pImportDialog == nullptr) { - pImportDialog = new CImportFeed(&m_feeds); - pImportDialog->Show(); - pImportDialog->SetParent(m_hwnd); - } + ImportFeeds(WPARAM(m_hwnd), 0); } void COptionsMain::OnExportButtonClick(CCtrlBase*) { - if (pExportDialog == nullptr) { - pExportDialog = new CExportFeed(); - pExportDialog->Show(); - pExportDialog->SetParent(m_hwnd); - } + ExportFeeds(WPARAM(m_hwnd), 0); } void COptionsMain::OnFeedListItemChanged(CCtrlListView::TEventInfo *evt) |