Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version
Go to top

WebResourceProvider

, 23 Mar 2007
A framework to allow public web services to be used as objects in your application.
// DlgLinkChecker.cpp : implementation file
//

#include "stdafx.h"
#include "WebResourceProvider_Demo.h"
#include "DlgLinkChecker.h"
#include "LinkProvider.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CDlgLinkChecker dialog


CDlgLinkChecker::CDlgLinkChecker(CWnd* pParent /*=NULL*/)
	: CDialog(CDlgLinkChecker::IDD, pParent)
{
	//{{AFX_DATA_INIT(CDlgLinkChecker)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT

  m_bStopCheck = false;
}


void CDlgLinkChecker::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CDlgLinkChecker)
		// NOTE: the ClassWizard will add DDX and DDV calls here
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CDlgLinkChecker, CDialog)
	//{{AFX_MSG_MAP(CDlgLinkChecker)
	ON_BN_CLICKED(BUTTON_GET_LINKS, OnGetLinks)
	ON_BN_CLICKED(BUTTON_CHECK_LINKS, OnCheckLinks)
	ON_BN_CLICKED(BUTTON_EXIT, OnExit)
	ON_BN_CLICKED(BUTTON_STOP_CHECK, OnStopCheck)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CDlgLinkChecker message handlers

void CDlgLinkChecker::OnGetLinks() 
{
  // Get url to check - complain if none
  CString strUrl;
  GetDlgItemText (EDIT_URL, strUrl);
  strUrl.TrimLeft();
  strUrl.TrimRight();
  if (strUrl.IsEmpty()) {
      AfxMessageBox ("Please enter a URL.");
      GetDlgItem (EDIT_URL)->SetFocus();
      return;
  }

  // Get links and m_images at target url - return if unable.
  LinkProvider  linkProvider;
  linkProvider.setUrl (strUrl);
  m_documents.RemoveAll();
  m_images.RemoveAll();
  AfxGetApp()->DoWaitCursor (1);
  linkProvider.getLinks (m_documents, m_images);
  AfxGetApp()->DoWaitCursor (-1);
  if (linkProvider.getFetchStatus() != 0) {
      AfxMessageBox ("Unable to get links at url.");
      return;
  }

  // Initialize GUI
  ((CProgressCtrl *) GetDlgItem (IDC_PROGBAR))->SetPos (0);
  SetDlgItemText (TEXT_PROGRESS, "");
  ((CListBox *) GetDlgItem (LIST_BROKEN_LINKS))->ResetContent();

  // Add href links to list box
  CListBox* pListBox = (CListBox *) GetDlgItem (LIST_HREFS);
  ASSERT (pListBox != NULL);
  pListBox->ResetContent();
  if (m_documents.GetSize() > 0) {
      for (long nDocs=0; (nDocs < m_documents.GetSize()); nDocs++) {
          pListBox->AddString (m_documents.GetAt (nDocs));
      }
  }
  SetDlgItemInt (TEXT_TOTAL_HREFS, m_documents.GetSize());

  // Add m_images to list box
  pListBox = (CListBox *) GetDlgItem (LIST_IMAGES);
  ASSERT (pListBox != NULL);
  pListBox->ResetContent();
  if (m_images.GetSize() > 0) {
      for (long nImages=0; (nImages < m_images.GetSize()); nImages++) {
          pListBox->AddString (m_images.GetAt (nImages));
      }
  }
  SetDlgItemInt (TEXT_TOTAL_IMAGES, m_images.GetSize());

  // Enable checking
  BOOL bEnableChecking = (m_documents.GetSize() > 0) || (m_images.GetSize() > 0);
  GetDlgItem (BUTTON_CHECK_LINKS)->EnableWindow (bEnableChecking);
}

void CDlgLinkChecker::OnCheckLinks() 
{
  CListBox* pListBox = (CListBox *) GetDlgItem (LIST_BROKEN_LINKS);
  ASSERT (pListBox != 0);
  long nHrefs = m_documents.GetSize();
  long nTotalLinks = nHrefs + m_images.GetSize();
  allowChecking (false);

  // Check HREF links
  m_bStopCheck = false;
  int nIndex = 0;
  for (nIndex=0; (nIndex < m_documents.GetSize()); nIndex++) {
      CString strUrl = m_documents.GetAt (nIndex);
      SetDlgItemText (TEXT_URL, strUrl);
      if (!CWebResourceProvider::urlExists (strUrl)) {
          pListBox->AddString (strUrl);
          SetDlgItemInt (TEXT_TOTAL_BROKEN, pListBox->GetCount());
      }

      // Update progress - quit if operation is cancelled
      updateProgress (nIndex, nTotalLinks);
      if (isCancelled()) {
          if (AfxMessageBox ("Stop checking links?", MB_ICONQUESTION | MB_YESNO) == IDYES) {
              allowChecking (true);
              updateProgress (0, nTotalLinks);
              SetDlgItemText (TEXT_PROGRESS, "");
              SetDlgItemText (TEXT_URL, "");
              return;
          }
      }
  }

  // Check image links
  for (nIndex=0; (nIndex < m_images.GetSize()); nIndex++) {
      CString strUrl = m_images.GetAt (nIndex);
      SetDlgItemText (TEXT_URL, strUrl);
      if (!CWebResourceProvider::urlExists (strUrl)) {
          pListBox->AddString (strUrl);
          SetDlgItemInt (TEXT_TOTAL_BROKEN, pListBox->GetCount());
      }

      // Update progress - quit if operation is cancelled
      updateProgress (nIndex + nHrefs, nTotalLinks);
      if (isCancelled()) {
          if (AfxMessageBox ("Stop checking links?", MB_ICONQUESTION | MB_YESNO) == IDYES) {
              allowChecking (true);
              SetDlgItemText (TEXT_PROGRESS, "");
              SetDlgItemText (TEXT_URL, "");
              return;
          }
      }
  }

  // Update progress to 100%
  updateProgress (nTotalLinks, nTotalLinks);
  SetDlgItemText (TEXT_URL, "");
  allowChecking (true);
}

void CDlgLinkChecker::OnOK() 
{
  OnGetLinks();
}

void CDlgLinkChecker::OnCancel() 
{
  return;
}

void CDlgLinkChecker::OnExit() 
{
  CDialog::OnOK();
}

void CDlgLinkChecker::OnStopCheck() 
{
  m_bStopCheck = true;
  GetDlgItem (BUTTON_STOP_CHECK)->EnableWindow (FALSE);
}

/////////////////////////////////////////////////////////////////////////////
// CDlgLinkChecker dialog helpers

void CDlgLinkChecker::allowChecking
  (bool bAllow)
{
  BOOL bAllowChecking = (bAllow ? TRUE : FALSE);
  GetDlgItem (BUTTON_GET_LINKS)->EnableWindow (bAllowChecking);
  GetDlgItem (BUTTON_CHECK_LINKS)->EnableWindow (bAllowChecking);
  GetDlgItem (BUTTON_STOP_CHECK)->EnableWindow (!bAllowChecking);
  GetDlgItem (BUTTON_EXIT)->EnableWindow (bAllowChecking);
}

void CDlgLinkChecker::updateProgress
  (long nProgress,
   long nTotal)
{
  long nPercentage = (nProgress * 100) / nTotal;
  ((CProgressCtrl *) GetDlgItem (IDC_PROGBAR))->SetPos (nPercentage);
  CString strProgress;
  strProgress.Format ("%d%%", nPercentage);
  SetDlgItemText (TEXT_PROGRESS, strProgress);
}

bool CDlgLinkChecker::isCancelled()
{
  // Pump message queue
  MSG   msg;
  while (::PeekMessage (&msg, NULL, 0, 0, PM_NOREMOVE))
        AfxGetThread()->PumpMessage();
  return (m_bStopCheck);
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Ravi Bhavnani
Technical Lead
Canada Canada
Ravi Bhavnani is an ardent fan of Microsoft technologies who loves building Windows apps, especially PIMs, system utilities, and things that go bump on the Internet. During his career, Ravi has developed expert systems, desktop imaging apps, marketing automation software, EDA tools, a platform to help people find, analyze and understand information, trading software for institutional investors and advanced data visualization solutions. He currently works for a company that provides enterprise workforce management solutions to large clients.
 
His interests include the .NET framework, reasoning systems, financial analysis and algorithmic trading, NLP, HCI and UI design. Ravi holds a BS in Physics and Math and an MS in Computer Science and was a Microsoft MVP (C++ and C# in 2006 and 2007). He is also the co-inventor of 2 patents on software security and generating data visualization dashboards. His claim to fame is that he crafted CodeProject's "joke" forum post icon.
 
Ravi's biggest fear is that one day he might actually get a life, although the chances of that happening seem extremely remote.
Follow on   Google+   LinkedIn

| Advertise | Privacy | Mobile
Web04 | 2.8.140916.1 | Last Updated 23 Mar 2007
Article Copyright 2002 by Ravi Bhavnani
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid